Submission #94333

#TimeUsernameProblemLanguageResultExecution timeMemory
94333dalgerokCities (BOI16_cities)C++14
74 / 100
6015 ms44408 KiB
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 5, M = (1 << 5) + 5; const long long INF = 1e18; int n, k, m, val[N]; vector < pair < int, int > > g[N]; long long d[M][N]; int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> k >> m; for(int i = 0; i < (1 << k); i++){ for(int j = 1; j <= n; j++){ d[i][j] = INF; } } for(int i = 0; i < k; i++){ int x; cin >> x; val[x] = (1 << i); d[val[x]][x] = 0; } for(int i = 1; i <= m; i++){ int x, y, z; cin >> x >> y >> z; g[x].push_back(make_pair(y, z)); g[y].push_back(make_pair(x, z)); } for(int mask = 1; mask < (1 << k); mask++){ set < pair < long long, int > > q; for(int i = 0; i < mask; i++){ if((mask | i) != mask){ continue; } for(int j = 1; j <= n; j++){ d[mask][j] = min(d[mask][j], d[i][j] + d[(mask ^ i)][j]); } } for(int i = 1; i <= n; i++){ if(d[mask][i] != INF){ q.insert(make_pair(d[mask][i], i)); } } while(!q.empty()){ int v = q.begin()->second; q.erase(q.begin()); for(auto it : g[v]){ int to = it.first, len = it.second; if(d[mask][to] > d[mask][v] + len){ q.erase(make_pair(d[mask][to], to)); d[mask][to] = d[mask][v] + len; q.insert(make_pair(d[mask][to], to)); } } } } long long ans = INF; for(int i = 1; i <= n; i++){ ans = min(ans, d[(1 << k) - 1][i]); } cout << ans; }
#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...