#include <bits/stdc++.h>
typedef long long ll;
const ll inf = 1e15;
struct Edge {
ll u, v, w;
};
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);
std::vector<Edge> edges;
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});
edges.push_back({u, v, w});
}
std::vector<ll> A(k);
std::vector<bool> special(n + 1);
for (auto &i : A) {
std::cin >> i;
special[i] = true;
}
auto get_best = [&]() {
std::priority_queue<std::pair<ll, std::pair<ll, ll>>> pq;
std::vector<ll> d(n + 1, inf);
for (auto &i : A) {
pq.push({0, {i, i}});
d[i] = 0;
}
std::vector<std::pair<ll, ll>> closest_special(n + 1);
std::vector<bool> vis(n + 1);
while (!pq.empty()) {
ll node = pq.top().second.first, source = pq.top().second.second,
dist = -pq.top().first;
pq.pop();
if (vis[node]) {
continue;
}
vis[node] = true;
closest_special[node] = {source, dist};
for (auto &[i, i_d] : adj[node]) {
if (dist + i_d < d[i]) {
d[i] = dist + i_d;
pq.push({-d[i], {i, source}});
}
}
}
std::pair<ll, std::pair<ll, ll>> ans = {inf, {0, 0}};
for (auto &[u, v, w] : edges) {
if (special[u] and special[v]) {
ans = std::min(ans, {w, {u, v}});
}
}
for (auto &[u, v, w] : edges) {
if (closest_special[u].first != closest_special[v].first) {
ans = std::min(
ans, {closest_special[u].second + w + closest_special[v].second,
{closest_special[u].first, closest_special[v].first}});
}
}
return ans;
};
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;
};
auto best = get_best();
auto &[u, v] = best.second;
special[u] = special[v] = false;
A.erase(std::remove(A.begin(), A.end(), u), A.end());
A.erase(std::remove(A.begin(), A.end(), v), A.end());
auto second_best = get_best();
ll ans = best.first + second_best.first;
auto sssp1 = sssp(u);
std::vector<std::pair<ll, ll>> best1;
for (ll x = 1; x <= n; ++x) {
if (x != v and special[x]) {
best1.push_back({sssp1[x], x});
}
}
auto sssp2 = sssp(v);
std::vector<std::pair<ll, ll>> best2;
for (ll y = 1; y <= n; ++y) {
if (y != u and special[y]) {
best2.push_back({sssp1[y], y});
}
}
std::sort(best1.begin(), best1.end());
std::sort(best2.begin(), best2.end());
for (ll i = 0; i < std::min(best1.size(), size_t(5)); ++i) {
for (ll j = 0; j < std::min(best2.size(), size_t(5)); ++j) {
if (best1[i].second != best2[j].second) {
ans = std::min(ans, best1[i].first + best2[j].first);
}
}
}
std::cout << ans << '\n';
}
Compilation message
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:120:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'const long unsigned int' [-Wsign-compare]
120 | for (ll i = 0; i < std::min(best1.size(), size_t(5)); ++i) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:121:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'const long unsigned int' [-Wsign-compare]
121 | for (ll j = 0; j < std::min(best2.size(), size_t(5)); ++j) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
10756 KB |
Output is correct |
2 |
Correct |
4 ms |
5976 KB |
Output is correct |
3 |
Correct |
813 ms |
210664 KB |
Output is correct |
4 |
Correct |
412 ms |
88316 KB |
Output is correct |
5 |
Correct |
104 ms |
21108 KB |
Output is correct |
6 |
Correct |
85 ms |
17032 KB |
Output is correct |
7 |
Correct |
114 ms |
23556 KB |
Output is correct |
8 |
Correct |
52 ms |
11016 KB |
Output is correct |
9 |
Correct |
68 ms |
15616 KB |
Output is correct |
10 |
Correct |
48 ms |
12608 KB |
Output is correct |
11 |
Correct |
809 ms |
211280 KB |
Output is correct |
12 |
Correct |
50 ms |
13320 KB |
Output is correct |
13 |
Correct |
235 ms |
55292 KB |
Output is correct |
14 |
Correct |
107 ms |
22808 KB |
Output is correct |
15 |
Correct |
779 ms |
209432 KB |
Output is correct |
16 |
Correct |
25 ms |
9996 KB |
Output is correct |
17 |
Correct |
574 ms |
138664 KB |
Output is correct |
18 |
Correct |
6 ms |
5784 KB |
Output is correct |
19 |
Correct |
840 ms |
211532 KB |
Output is correct |
20 |
Correct |
100 ms |
23040 KB |
Output is correct |
21 |
Correct |
81 ms |
20220 KB |
Output is correct |
22 |
Correct |
42 ms |
12036 KB |
Output is correct |
23 |
Correct |
18 ms |
8464 KB |
Output is correct |
24 |
Correct |
561 ms |
149192 KB |
Output is correct |
25 |
Correct |
78 ms |
15888 KB |
Output is correct |
26 |
Correct |
36 ms |
11532 KB |
Output is correct |
27 |
Correct |
54 ms |
14080 KB |
Output is correct |
28 |
Correct |
9 ms |
7140 KB |
Output is correct |
29 |
Correct |
96 ms |
22868 KB |
Output is correct |
30 |
Correct |
193 ms |
43480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |