Submission #1093106

#TimeUsernameProblemLanguageResultExecution timeMemory
1093106juicyCities (BOI16_cities)C++17
100 / 100
1382 ms41584 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif int main() { ios::sync_with_stdio(false); cin.tie(nullptr); const long long inf = 1e18; int n, k, m; cin >> n >> k >> m; vector<int> city(k); for (int &u : city) { cin >> u; --u; } vector<vector<array<int, 2>>> g(n); while (m--) { int u, v, c; cin >> u >> v >> c; --u, --v; g[u].push_back({v, c}); g[v].push_back({u, c}); } auto dijkstra = [&](vector<long long> &d) { using T = pair<long long, int>; priority_queue<T, vector<T>, greater<T>> pq; for (int i = 0; i < n; ++i) { pq.push({d[i], i}); } while (pq.size()) { auto [c, u] = pq.top(); pq.pop(); if (c != d[u]) { continue; } for (auto [v, w] : g[u]) { if (d[v] > c + w) { pq.push({d[v] = c + w, v}); } } } return d; }; vector d(1 << k, vector<long long>(n, inf)); for (int i = 0; i < k; ++i) { d[1 << i][city[i]] = 0; } for (int m = 1; m < 1 << k; ++m) { for (int i = 0; i < k; ++i) { if (m >> i & 1) { for (int j = 0; j < n; ++j) { d[m][j] = min(d[m][j], d[m - (1 << i)][j] + d[1 << i][j]); } } } dijkstra(d[m]); } long long res = inf; for (int i = 0; i < k; ++i) { res = min(res, d.back()[city[i]]); } cout << res; 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...