제출 #1126690

#제출 시각아이디문제언어결과실행 시간메모리
1126690ShadowSharkCities (BOI16_cities)C++20
100 / 100
1783 ms40016 KiB
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 5; const long long inf = 1e14; vector<pair<int, int>> adj[maxN]; long long dis[32][maxN]; int n, k, m, pos[10]; void dijkstra(int mask) { priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq; for (int i = 1; i <= n; i++) if (dis[mask][i] < inf) pq.push({dis[mask][i], i}); while (pq.size() > 0) { auto [len, u] = pq.top(); pq.pop(); if (dis[mask][u] != len) continue; for (auto [v, w]: adj[u]) if (dis[mask][v] > len + w) { dis[mask][v] = len + w; pq.push({dis[mask][v], v}); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> m; for (int i = 1; i <= k; i++) cin >> pos[i]; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } for (int mask = 1; mask < (1 << k); mask++) for (int i = 1; i <= n; i++) dis[mask][i] = inf; for (int i = 1; i <= k; i++) dis[(1 << (i - 1))][pos[i]] = 0; for (int mask = 1; mask < (1 << k); mask++) { for (int i = 1; i <= n; i++) for (int submask = mask; submask > 0; submask = (submask - 1) & mask) dis[mask][i] = min(dis[mask][i], dis[submask][i] + dis[mask ^ submask][i]); dijkstra(mask); } cout << *min_element(dis[(1 << k) - 1] + 1, dis[(1 << k) - 1] + n + 1); 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...