제출 #554320

#제출 시각아이디문제언어결과실행 시간메모리
554320Soumya1Cities (BOI16_cities)C++17
74 / 100
6067 ms91424 KiB
#include <bits/stdc++.h> #ifdef __LOCAL__ #include <debug_local.h> #endif using namespace std; const int mxN = 1e5 + 5; const long long inf = 1e18; using ll = long long; int idx[mxN]; vector<pair<int, int>> ad[mxN]; vector<int> terminals; ll dist[5][mxN]; ll d[mxN][32]; int n, k; void dijkstra(int s) { int t = idx[s]; for (int i = 1; i <= n; i++) dist[t][i] = inf; priority_queue<pair<ll, int>> pq; pq.push({0, s}); dist[t][s] = 0; while (!pq.empty()) { auto [dd, u] = pq.top(); pq.pop(); if (dist[t][u] != -dd) continue; for (auto [v, w] : ad[u]) { if (dist[t][v] > dist[t][u] + w) { dist[t][v] = dist[t][u] + w; pq.push({-dist[t][v], v}); } } } } ll solve(int s) { for (int i = 1; i <= n; i++) { for (int j = 0; j < (1 << k); j++) d[i][j] = inf; } priority_queue<tuple<ll, int, int>> pq; pq.push({0, s, 1 << idx[s]}); d[s][1 << idx[s]] = 0; while (!pq.empty()) { auto [_, u, mask] = pq.top(); pq.pop(); if (d[u][mask] != -_) continue; for (int j = 0; j < k; j++) { if (mask >> j & 1) continue; int new_mask = mask | (1 << j); if (d[u][new_mask] > d[u][mask] + dist[j][u]) { d[u][new_mask] = d[u][mask] + dist[j][u]; pq.push({-d[u][new_mask], u, new_mask}); } } for (auto [v, w] : ad[u]) { if (d[v][mask] > d[u][mask] + w) { d[v][mask] = d[u][mask] + w; pq.push({-d[v][mask], v, mask}); } } } ll ans = inf; for (int i = 1; i <= n; i++) { ans = min(ans, d[i][(1 << k) - 1]); } return ans; } void testCase() { int m; cin >> n >> k >> m; for (int i = 0; i < k; i++) { int x; cin >> x; terminals.push_back(x); idx[x] = i; } while (m--) { int u, v, w; cin >> u >> v >> w; ad[u].push_back({v, w}); ad[v].push_back({u, w}); } for (int t : terminals) { dijkstra(t); } ll ans = inf; for (int t : terminals) { ans = min(ans, solve(t)); } cout << ans << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); testCase(); 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...