Submission #547294

#TimeUsernameProblemLanguageResultExecution timeMemory
547294JomnoiCities (BOI16_cities)C++17
100 / 100
1784 ms44784 KiB
#include <bits/stdc++.h> #define DEBUG 0 using namespace std; const int MAX_N = 1e5 + 10; const long long INF = 1e18 + 7; int n; vector <pair <int, int>> graph[MAX_N]; long long dist[32][MAX_N]; void Dijkstra(int mask) { priority_queue <pair <long long, int>> pq; for(int i = 1; i <= n; i++) { if(dist[mask][i] != INF) { pq.emplace(-dist[mask][i], i); } } while(!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if(dist[mask][u] < -d) { continue; } for(auto [v, w] : graph[u]) { if(dist[mask][u] + w < dist[mask][v]) { dist[mask][v] = dist[mask][u] + w; pq.emplace(-dist[mask][v], v); } } } } int main() { cin.tie(0)->sync_with_stdio(0); int k, m; cin >> n >> k >> m; for(int i = 0; i < (1<<k); i++) { fill(dist[i], dist[i] + n + 1, INF); } for(int i = 0; i < k; i++) { int x; cin >> x; dist[1<<i][x] = 0; } while(m--) { int a, b, c; cin >> a >> b >> c; graph[a].emplace_back(b, c); graph[b].emplace_back(a, c); } for(int mask = 1; mask < (1<<k); mask++) { if(__builtin_popcount(mask) > 1) { for(int s = mask; s > 0; s = (s - 1) & mask) { for(int i = 1; i <= n; i++) { dist[mask][i] = min(dist[mask][i], dist[s][i] + dist[mask ^ s][i]); } } } Dijkstra(mask); } long long ans = INF; for(int i = 1; i <= n; i++) { ans = min(ans, dist[(1<<k) - 1][i]); } cout << ans; return 0; }
#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...