#include <bits/stdc++.h>
typedef long long ll;
const ll inf = 1e15;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, m, k;
std::cin >> n >> m >> k;
std::vector<std::vector<std::pair<ll, ll>>> adj(n + 1);
for (ll i = 0, u, v, w; i < m; ++i) {
std::cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
std::vector<ll> A(k);
std::vector<bool> special(n + 1);
for (auto &i : A) {
std::cin >> i;
special[i] = true;
}
auto sssp = [&](ll x) {
std::priority_queue<std::pair<ll, ll>> pq;
pq.push({0, x});
std::vector<ll> ans(n + 1, inf);
std::vector<bool> vis(n + 1);
ans[x] = 0;
while (!pq.empty()) {
ll node = pq.top().second, d = -pq.top().first;
pq.pop();
if (vis[node]) {
continue;
}
vis[node] = true;
for (auto &[i, dist] : adj[node]) {
if (d + dist < ans[i]) {
ans[i] = d + dist;
pq.push({-ans[i], i});
}
}
}
return ans;
};
std::vector<std::vector<ll>> dist(n + 1);
std::vector<std::vector<std::pair<ll, ll>>> sorted_dists(n + 1);
for (ll i = 1; i <= n; ++i) {
dist[i] = sssp(i);
for (ll j = 1; j <= n; ++j) {
if (i == j or !special[j]) {
continue;
}
sorted_dists[i].push_back({dist[i][j], j});
}
std::sort(sorted_dists[i].begin(), sorted_dists[i].end());
}
ll ans = inf;
for (ll p_a = 0; p_a < k; ++p_a) {
for (ll p_b = p_a + 1; p_b < k; ++p_b) {
for (ll p_c = p_b + 1; p_c < k; ++p_c) {
ll a = A[p_a], b = A[p_b], c = A[p_c];
ll ptr = 0;
while (ptr < sorted_dists[c].size() and
(sorted_dists[c][ptr].second == a or
sorted_dists[c][ptr].second == b)) {
ptr++;
}
if (ptr < sorted_dists[c].size()) {
ans = std::min(ans, dist[a][b] + sorted_dists[c][ptr].first);
}
}
}
}
std::cout << ans << '\n';
}
Compilation message
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:68:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | while (ptr < sorted_dists[c].size() and
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:73:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | if (ptr < sorted_dists[c].size()) {
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
536 KB |
Output is correct |
15 |
Correct |
1 ms |
496 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
536 KB |
Output is correct |
15 |
Correct |
1 ms |
496 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
6 ms |
2396 KB |
Output is correct |
32 |
Correct |
13 ms |
2440 KB |
Output is correct |
33 |
Correct |
19 ms |
2396 KB |
Output is correct |
34 |
Correct |
3 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
67 ms |
6556 KB |
Output is correct |
37 |
Correct |
107 ms |
6636 KB |
Output is correct |
38 |
Correct |
23 ms |
2644 KB |
Output is correct |
39 |
Correct |
185 ms |
8016 KB |
Output is correct |
40 |
Correct |
112 ms |
7760 KB |
Output is correct |
41 |
Correct |
24 ms |
2504 KB |
Output is correct |
42 |
Correct |
122 ms |
7784 KB |
Output is correct |
43 |
Correct |
60 ms |
6652 KB |
Output is correct |
44 |
Correct |
24 ms |
2392 KB |
Output is correct |
45 |
Correct |
2 ms |
2392 KB |
Output is correct |
46 |
Correct |
237 ms |
11916 KB |
Output is correct |
47 |
Correct |
102 ms |
7248 KB |
Output is correct |
48 |
Correct |
178 ms |
8528 KB |
Output is correct |
49 |
Correct |
201 ms |
11548 KB |
Output is correct |
50 |
Correct |
20 ms |
2616 KB |
Output is correct |
51 |
Correct |
38 ms |
6484 KB |
Output is correct |
52 |
Correct |
19 ms |
2392 KB |
Output is correct |
53 |
Correct |
146 ms |
5460 KB |
Output is correct |
54 |
Correct |
194 ms |
8020 KB |
Output is correct |
55 |
Correct |
13 ms |
2508 KB |
Output is correct |
56 |
Correct |
3 ms |
2396 KB |
Output is correct |
57 |
Correct |
17 ms |
2640 KB |
Output is correct |
58 |
Correct |
224 ms |
9812 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
45 ms |
6480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4455 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
536 KB |
Output is correct |
15 |
Correct |
1 ms |
496 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
6 ms |
2396 KB |
Output is correct |
32 |
Correct |
13 ms |
2440 KB |
Output is correct |
33 |
Correct |
19 ms |
2396 KB |
Output is correct |
34 |
Correct |
3 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
67 ms |
6556 KB |
Output is correct |
37 |
Correct |
107 ms |
6636 KB |
Output is correct |
38 |
Correct |
23 ms |
2644 KB |
Output is correct |
39 |
Correct |
185 ms |
8016 KB |
Output is correct |
40 |
Correct |
112 ms |
7760 KB |
Output is correct |
41 |
Correct |
24 ms |
2504 KB |
Output is correct |
42 |
Correct |
122 ms |
7784 KB |
Output is correct |
43 |
Correct |
60 ms |
6652 KB |
Output is correct |
44 |
Correct |
24 ms |
2392 KB |
Output is correct |
45 |
Correct |
2 ms |
2392 KB |
Output is correct |
46 |
Correct |
237 ms |
11916 KB |
Output is correct |
47 |
Correct |
102 ms |
7248 KB |
Output is correct |
48 |
Correct |
178 ms |
8528 KB |
Output is correct |
49 |
Correct |
201 ms |
11548 KB |
Output is correct |
50 |
Correct |
20 ms |
2616 KB |
Output is correct |
51 |
Correct |
38 ms |
6484 KB |
Output is correct |
52 |
Correct |
19 ms |
2392 KB |
Output is correct |
53 |
Correct |
146 ms |
5460 KB |
Output is correct |
54 |
Correct |
194 ms |
8020 KB |
Output is correct |
55 |
Correct |
13 ms |
2508 KB |
Output is correct |
56 |
Correct |
3 ms |
2396 KB |
Output is correct |
57 |
Correct |
17 ms |
2640 KB |
Output is correct |
58 |
Correct |
224 ms |
9812 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
45 ms |
6480 KB |
Output is correct |
61 |
Runtime error |
4455 ms |
1048576 KB |
Execution killed with signal 9 |
62 |
Halted |
0 ms |
0 KB |
- |