Submission #1130983

#TimeUsernameProblemLanguageResultExecution timeMemory
1130983RandomUserCities (BOI16_cities)C++20
74 / 100
6085 ms137776 KiB
#include <bits/stdc++.h> #define ar array using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int maxn = 1e5 + 5; ll dist[5][maxn], dp[1<<5][maxn], imp[5]; vector<pii> G[maxn]; signed main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; for(int i=0; i<k; i++) cin >> imp[i]; for(int i=1; i<=m; i++) { int a, b, w; cin >> a >> b >> w; G[a].push_back({ b, w }); G[b].push_back({ a, w }); } for(int i=0; i<k; i++) { priority_queue<pll, vector<pll>, greater<> > pq; for(int j=1; j<=n; j++) dist[i][j] = 1e18; pq.push({ 0, imp[i] }); dist[i][imp[i]] = 0; while(!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); for(auto [v, w] : G[u]) if(dist[i][v] > d + w) pq.push({ dist[i][v] = d + w, v }); } } priority_queue<ar<ll, 3>, vector<ar<ll, 3> >, greater<> > pq; for(int i=1; i<=n; i++) for(int j=0; j<(1<<k); j++) dp[j][i] = 1e18; for(int i=0; i<k; i++) pq.push({ dp[1<<i][imp[i]] = 0, imp[i], 1<<i }); while(!pq.empty()) { auto [d, u, s] = pq.top(); pq.pop(); if(d != dp[s][u]) continue; for(auto [v, w] : G[u]) if(dp[s][v] > d + w) pq.push({ dp[s][v] = d + w, v, s }); for(int i=0; i<k; i++) if(dp[s|(1<<i)][u] > d + dist[i][u]) pq.push({ dp[s|(1<<i)][u] = d + dist[i][u], u, s|(1<<i) }); } ll ans = 1e18; for(int i=1; i<=n; i++) ans = min(ans, dp[(1<<k)-1][i]); cout << ans << '\n'; 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...