This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |