#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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
55960 KB |
Output is correct |
2 |
Correct |
14 ms |
55900 KB |
Output is correct |
3 |
Correct |
15 ms |
55896 KB |
Output is correct |
4 |
Correct |
15 ms |
55900 KB |
Output is correct |
5 |
Correct |
24 ms |
55900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
708 ms |
70348 KB |
Output is correct |
2 |
Correct |
867 ms |
70716 KB |
Output is correct |
3 |
Correct |
394 ms |
63432 KB |
Output is correct |
4 |
Correct |
56 ms |
64452 KB |
Output is correct |
5 |
Correct |
331 ms |
70652 KB |
Output is correct |
6 |
Correct |
57 ms |
64208 KB |
Output is correct |
7 |
Correct |
20 ms |
56156 KB |
Output is correct |
8 |
Correct |
19 ms |
56156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
56252 KB |
Output is correct |
2 |
Correct |
22 ms |
56156 KB |
Output is correct |
3 |
Correct |
17 ms |
56000 KB |
Output is correct |
4 |
Correct |
18 ms |
56156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2108 ms |
70340 KB |
Output is correct |
2 |
Correct |
2083 ms |
74436 KB |
Output is correct |
3 |
Correct |
1273 ms |
63432 KB |
Output is correct |
4 |
Correct |
864 ms |
70008 KB |
Output is correct |
5 |
Correct |
235 ms |
68552 KB |
Output is correct |
6 |
Correct |
70 ms |
66256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5769 ms |
73724 KB |
Output is correct |
2 |
Correct |
5706 ms |
76664 KB |
Output is correct |
3 |
Correct |
4601 ms |
74296 KB |
Output is correct |
4 |
Correct |
2360 ms |
63440 KB |
Output is correct |
5 |
Correct |
1771 ms |
70336 KB |
Output is correct |
6 |
Correct |
557 ms |
68548 KB |
Output is correct |
7 |
Correct |
121 ms |
66764 KB |
Output is correct |