#include <iostream>
#include <climits>
#include <queue>
using namespace std;
typedef long long int Z;
int n, k, m;
vector<int> imp;
vector<vector<pair<int, Z>>> G;
Z d[32][202020];
Z huge = LLONG_MAX / 3;
bool seen[202020];
int main() {
cin.sync_with_stdio(false);
cin >> n >> k >> m;
imp.resize(k);
for(int i = 0; i < k; ++i) {
cin >> imp[i];
--imp[i];
}
G.resize(n);
for(int i = 0; i < m; ++i) {
int a, b;
Z c;
cin >> a >> b >> c;
--a;
--b;
G[a].emplace_back(b, c);
G[b].emplace_back(a, c);
}
fill((Z*)d, (Z*)d + 32 * 202020, huge);
for(int i = 0; i < k; ++i) {
d[1 << i][imp[i]] = 0;
}
priority_queue<pair<Z, int>> Q;
for(int c = 1; c < (1 << k); ++c) {
for(int a = 0; a < c; ++a) {
if((a | c) != c) continue;
int b = c ^ a;
if(b > a) continue;
for(int v = 0; v < n; ++v) {
d[c][v] = min(d[c][v], d[a][v] + d[b][v]);
}
}
fill(seen, seen + n, false);
for(int v = 0; v < n; ++v) {
if(d[c][v] == huge) continue;
Q.emplace(-d[c][v], v);
}
while(!Q.empty()) {
Z cost = -Q.top().first;
int v = Q.top().second;
Q.pop();
if(seen[v]) continue;
seen[v] = true;
for(auto edge : G[v]) {
Z ec = cost + edge.second;
if(ec < d[c][edge.first]) {
d[c][edge.first] = ec;
Q.emplace(-ec, edge.first);
}
}
}
}
Z res = huge;
for(int v = 0; v < n; ++v) {
res = min(res, d[(1 << k) - 1][v]);
}
cout << res << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
52884 KB |
Output is correct |
2 |
Correct |
9 ms |
52884 KB |
Output is correct |
3 |
Correct |
3 ms |
52884 KB |
Output is correct |
4 |
Correct |
9 ms |
52884 KB |
Output is correct |
5 |
Correct |
3 ms |
52884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
663 ms |
71744 KB |
Output is correct |
2 |
Correct |
659 ms |
71628 KB |
Output is correct |
3 |
Correct |
393 ms |
63588 KB |
Output is correct |
4 |
Correct |
136 ms |
61856 KB |
Output is correct |
5 |
Correct |
413 ms |
71724 KB |
Output is correct |
6 |
Correct |
99 ms |
61856 KB |
Output is correct |
7 |
Correct |
13 ms |
53016 KB |
Output is correct |
8 |
Correct |
0 ms |
53016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
53016 KB |
Output is correct |
2 |
Correct |
9 ms |
53016 KB |
Output is correct |
3 |
Correct |
9 ms |
53016 KB |
Output is correct |
4 |
Correct |
16 ms |
53016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1383 ms |
71736 KB |
Output is correct |
2 |
Correct |
1369 ms |
71368 KB |
Output is correct |
3 |
Correct |
923 ms |
63584 KB |
Output is correct |
4 |
Correct |
686 ms |
67524 KB |
Output is correct |
5 |
Correct |
256 ms |
64168 KB |
Output is correct |
6 |
Correct |
153 ms |
65076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2723 ms |
71732 KB |
Output is correct |
2 |
Correct |
2456 ms |
71716 KB |
Output is correct |
3 |
Correct |
2146 ms |
71448 KB |
Output is correct |
4 |
Correct |
1693 ms |
63584 KB |
Output is correct |
5 |
Correct |
1376 ms |
67500 KB |
Output is correct |
6 |
Correct |
283 ms |
64180 KB |
Output is correct |
7 |
Correct |
109 ms |
65232 KB |
Output is correct |