Submission #653419

#TimeUsernameProblemLanguageResultExecution timeMemory
653419tvladm2009Cities (BOI16_cities)C++14
100 / 100
2512 ms51040 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll const int N = (int) 2e5 + 7; const int K = 5; const int INF = (ll) 1e18; int dp[1 << K][N], special[N]; vector<pair<int, int>> g[N]; bool vis[N]; int n, k, m; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> m; vector<int> cities(k + 1); for (int i = 1; i <= k; i++) { cin >> cities[i]; special[cities[i]] = i; } for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; g[x].push_back({y, z}); g[y].push_back({x, z}); } for (int mask = 0; mask < (1 << k); mask++) { for (int i = 0; i <= n; i++) { dp[mask][i] = INF; } } for (int i = 1; i <= k; i++) { dp[(1 << (i - 1))][cities[i]] = 0; } for (int i = 1; i <= n; i++) { dp[0][i] = 0; } for (int mask = 0; mask < (1 << k); mask++) { for (int i = 1; i <= n; i++) { for (int submask = mask; submask > 0; submask = (submask - 1) & mask) { ll newmask = (mask ^ submask); if (special[i]) { newmask ^= (1 << (special[i] - 1)); } dp[mask][i] = min(dp[mask][i], dp[newmask][i] + dp[submask][i]); } } priority_queue<pair<int, int>> pq; for (int i = 0; i <= n; i++) { vis[i] = 0; pq.push({-dp[mask][i], i}); } while (!pq.empty()) { pair<int, int> u = pq.top(); pq.pop(); if (vis[u.second]) { continue; } vis[u.second] = 1; for (auto &v : g[u.second]) { if (dp[mask][u.second] + v.second < dp[mask][v.first]) { dp[mask][v.first] = dp[mask][u.second] + v.second; pq.push({-dp[mask][v.first], v.first}); } } } } ll ret = INF; for (int i = 0; i <= n; i++) { ret = min(ret, dp[(1 << k) - 1][i]); } cout << ret; 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...