Submission #708047

#TimeUsernameProblemLanguageResultExecution timeMemory
708047stevancvCities (BOI16_cities)C++14
100 / 100
3151 ms48724 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 3e5 + 5; const ll linf = 1e18; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, m; cin >> n >> k >> m; int K = 1 << k; vector<vector<ll>> dp(n, vector<ll>(K, linf)); vector<int> imp(k); for (int i = 0; i < k; i++) { cin >> imp[i]; imp[i] -= 1; dp[imp[i]][1 << i] = 0; } vector<vector<pair<int, int>>> g(n); for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; u -= 1; v -= 1; g[u].push_back({v, w}); g[v].push_back({u, w}); } for (int mask = 0; mask < K; mask++) { for (int i = 0; i < n; i++) { for (int smask = mask; ; smask = (smask - 1) & mask) { smin(dp[i][mask], dp[i][smask] + dp[i][mask ^ smask]); if (smask == 0) break; } } priority_queue<pair<ll, int>> pq; for (int i = 0; i < n; i++) { if (dp[i][mask] == linf) continue; pq.push({-dp[i][mask], i}); } while (!pq.empty()) { ll d = -pq.top().first; int u = pq.top().second; pq.pop(); if (d > dp[u][mask]) continue; for (auto p : g[u]) { ll w = d + p.second; if (dp[p.first][mask] > w) { dp[p.first][mask] = w; pq.push({-w, p.first}); } } } } ll ans = linf; for (int i = 0; i < n; i++) smin(ans, dp[i][K - 1]); cout << ans << en; 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...