Submission #716770

# Submission time Handle Problem Language Result Execution time Memory
716770 2023-03-31T04:39:00 Z Spade1 Cities (BOI16_cities) C++14
100 / 100
3263 ms 46560 KB
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
//#include <grader.h>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
#define db double
using namespace std;

const int N = 1e5 + 10;

int n, k, m;
ll dp[N][32];
int city[5];
vector<pll> adj[N];
priority_queue<pll> pq;

void solve() {
    int n, k, m;cin >> n >> k >> m;
    for (int s = 0; s < (1<<k); ++s) for (int i = 1; i <= n; ++i) dp[i][s] = 1e18;
    for (int i = 0, u; i < k; ++i) {
        cin >> u;
        dp[u][(1<<i)] = 0;
    }
    for (int i = 1; i <= m; ++i) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].pb({v, w}); adj[v].pb({u, w});
    }

    for (int s = 1; s < (1<<k); ++s) {
        for (int j = 0; j < k; ++j) {
            if (s&(1<<j))
                for (int i = 1; i <= n; ++i)
                dp[i][s] = min(dp[i][s], dp[i][(1<<j)] + dp[i][s^(1<<j)]);
        }
        for (int i = 1; i <= n; ++i) pq.push({-dp[i][s], i});
        while (!pq.empty()) {
            int a = pq.top().nd; pq.pop();
            for (auto [b, w] : adj[a]) {
                if (dp[b][s] > dp[a][s] + w) {
                    dp[b][s] = dp[a][s] + w;
                    pq.push({-dp[b][s], b});
                }
            }
        }
    }

    ll ans = LLONG_MAX;
    for (int i = 1; i <= n; ++i) ans = min(ans, dp[i][(1<<k)-1]);
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Compilation message

cities.cpp: In function 'void solve()':
cities.cpp:44:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   44 |             for (auto [b, w] : adj[a]) {
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2680 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 823 ms 46532 KB Output is correct
2 Correct 802 ms 45668 KB Output is correct
3 Correct 561 ms 37200 KB Output is correct
4 Correct 86 ms 15436 KB Output is correct
5 Correct 447 ms 46560 KB Output is correct
6 Correct 77 ms 15336 KB Output is correct
7 Correct 5 ms 3028 KB Output is correct
8 Correct 3 ms 3076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3072 KB Output is correct
2 Correct 6 ms 3116 KB Output is correct
3 Correct 5 ms 2952 KB Output is correct
4 Correct 4 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1764 ms 46456 KB Output is correct
2 Correct 1585 ms 45584 KB Output is correct
3 Correct 1116 ms 37184 KB Output is correct
4 Correct 994 ms 32108 KB Output is correct
5 Correct 206 ms 18992 KB Output is correct
6 Correct 92 ms 17652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3263 ms 46468 KB Output is correct
2 Correct 3249 ms 46456 KB Output is correct
3 Correct 3081 ms 45564 KB Output is correct
4 Correct 2180 ms 37312 KB Output is correct
5 Correct 1884 ms 32004 KB Output is correct
6 Correct 325 ms 19120 KB Output is correct
7 Correct 108 ms 17748 KB Output is correct