Submission #1028811

# Submission time Handle Problem Language Result Execution time Memory
1028811 2024-07-20T09:01:21 Z phoenix Cities (BOI16_cities) C++17
100 / 100
3534 ms 48316 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 100100;
const int M = 200200;
const ll INF = 1e15;

int n, m, k;
vector<pair<int, ll>> g[N];
int special[5];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> n >> k >> m;
    
    for (int i = 0; i < k; i++) {
        cin >> special[i];
        special[i]--;
    }


    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    ll dp[(1 << k)][n] = {};
    for (int i = 1; i < (1 << k); i++) 
        for (int j = 0; j < n; j++)
            dp[i][j] = INF;
    
    for (int i = 0; i < k; i++)
        dp[(1 << i)][special[i]] = 0;
    
    for (int msk = 1; msk < (1 << k); msk++) {
        for (int v = 0; v < n; v++) 
            for (int sub = msk; sub; sub = (msk & sub - 1)) 
                dp[msk][v] = min(dp[msk][v], dp[sub][v] + dp[(msk ^ sub)][v]);
        set<pair<ll, int>> st;
        for (int i = 0; i < n; i++)
            st.insert({dp[msk][i], i});
        while (!st.empty()) {
            int x = st.begin()->second;
            st.erase(st.begin());
            if (dp[msk][x] == INF) break;
            for (auto [to, w] : g[x]) {
                if (dp[msk][to] > dp[msk][x] + w) {
                    st.erase({dp[msk][to], to});
                    dp[msk][to] = dp[msk][x] + w;
                    st.insert({dp[msk][to], to});
                }
            }
        } 
    }
    cout << *min_element(dp[(1 << k) - 1], dp[(1 << k) - 1] + n);
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:43:55: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   43 |             for (int sub = msk; sub; sub = (msk & sub - 1))
      |                                                   ~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2596 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 819 ms 29388 KB Output is correct
2 Correct 802 ms 28776 KB Output is correct
3 Correct 370 ms 22608 KB Output is correct
4 Correct 47 ms 15112 KB Output is correct
5 Correct 364 ms 26196 KB Output is correct
6 Correct 42 ms 14928 KB Output is correct
7 Correct 4 ms 2904 KB Output is correct
8 Correct 3 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2908 KB Output is correct
2 Correct 7 ms 3132 KB Output is correct
3 Correct 5 ms 2908 KB Output is correct
4 Correct 5 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1663 ms 36036 KB Output is correct
2 Correct 1588 ms 35008 KB Output is correct
3 Correct 716 ms 28752 KB Output is correct
4 Correct 804 ms 25936 KB Output is correct
5 Correct 196 ms 17080 KB Output is correct
6 Correct 53 ms 17240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3221 ms 48208 KB Output is correct
2 Correct 3534 ms 48316 KB Output is correct
3 Correct 3208 ms 47424 KB Output is correct
4 Correct 1649 ms 41404 KB Output is correct
5 Correct 1754 ms 32084 KB Output is correct
6 Correct 328 ms 18104 KB Output is correct
7 Correct 67 ms 17592 KB Output is correct