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;
const int N = 200'000 + 10,
K = (1 << 5);
int n, m, k;
int mk[N], it;
vector<pair<int, int>> ad[N];
long long f[N][K];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> m;
for (int i = 1; i <= k; ++i) {
int x; cin >> x;
mk[x] = (1 << it++);
}
for (int i = 1; i <= m; ++i) {
int u, v, w; cin >> u >> v >> w;
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
{ //dijsktra
using T = tuple<long long, int, int>;
priority_queue<T, vector<T>, greater<>> q;
auto sktra = [&](int st) {
for (int i = 1; i <= n; ++i) q.emplace(f[i][st], i, st);
while (q.size()) {
auto [d, u, mask] = q.top(); q.pop();
if (d != f[u][mask]) continue;
for (const auto& [v, w] : ad[u]) {
int nMask = mask | mk[v];
if (f[v][nMask] > d + w) q.emplace(f[v][nMask] = d + w, v, nMask);
}
}
};
memset(f, 14, sizeof f);
for (int i = 1; i <= n; ++i) f[i][mk[i]] = 0;
for (int mask = 0; mask < (1 << k); ++mask) {
for (int sub1 = 0; sub1 < (1 << k); ++sub1) {
int sub2 = mask ^ sub1;
for (int i = 1; i <= n; ++i)
f[i][mask] = min(f[i][mask], f[i][sub1] + f[i][sub2]);
}
sktra(mask);
}
}
long long answer = f[0][0];
for (int i = 1; i <= n; ++i) answer = min(answer, f[i][(1 << k) - 1]);
cout << answer << "\n";
}
# | 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... |