Submission #919173

#TimeUsernameProblemLanguageResultExecution timeMemory
919173phongCities (BOI16_cities)C++17
100 / 100
3004 ms47608 KiB
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>

#define ll long long
const int nmax = 1e5 + 5, dmax = 2e5 + 5;
const ll oo = 1e18, base = 311;
const int lg = 18, M =2e2 + 5;
const ll mod = 1e9 + 7;
#define pii pair<ll, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
using namespace std;

int n, k, m, a[nmax];
ll dp[1 << 5][nmax];
vector<pii> adj[nmax];

priority_queue<pii, vector<pii>, greater<pii>> q;
void bfs(int msk){
    for(int i = 1; i <= n; ++i){
        q.push({dp[msk][i], i});
    }

    while(q.size()){
        pii tmp = q.top();
        int u = tmp.se;
        q.pop();
        if(tmp.fi != dp[msk][u]) continue;
        for(auto [v, w] : adj[u]){
            if(dp[msk][u] + w < dp[msk][v]){
                dp[msk][v] = dp[msk][u] + w;
                q.push({dp[msk][v], v});
            }
        }
    }
}

void ckmin(ll &a, ll b){
    a = min(a, b);
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n >> k >> m;
    memset(dp, 0x3f, sizeof dp);

    for(int i = 0; i < k; ++i){
        cin >> a[i];
        dp[1 << i][a[i]] = 0;
    }
    for(int i = 1, u, v, c; i <= m; ++i){
        cin >> u >> v >> c;
        adj[u].push_back({v, c});
        adj[v].push_back({u, c});
    }
    for(int msk = 1; msk < (1 << k); ++msk){
        bfs(msk);
        for(int x = 1; x <= n; ++x){
            for(int i = 0; i < k; ++i){
                if(msk >> i & 1){
                    ckmin(dp[msk][x], dp[msk ^ (1 << i)][x] + dp[1 << i][x]);
                }
            }
        }
        bfs(msk);

    }
    ll ans = oo;
    for(int i = 1; i <= n; ++i) ckmin(ans, dp[(1 <<k) - 1][i]);
    cout << ans;

}
/*
5 2
3 2
2 5
3 4
3 1

*/

Compilation message (stderr)

cities.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   44 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...