Submission #94336

#TimeUsernameProblemLanguageResultExecution timeMemory
94336dalgerokCities (BOI16_cities)C++14
100 / 100
2673 ms44888 KiB
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 5, M = (1 << 5) + 5; const long long INF = 1e18; bool used[N]; 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++){ priority_queue < 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++){ used[i] = false; if(d[mask][i] != INF){ q.push(make_pair(-d[mask][i], i)); } } while(!q.empty()){ long long val = -q.top().first; int v = q.top().second; q.pop(); if(used[v]){ continue; } used[v] = true; for(auto it : g[v]){ int to = it.first; long long nxt = val + it.second; if(d[mask][to] > nxt){ d[mask][to] = nxt; q.push(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...