#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int N, M, K, ans = 1e16, dist[100005], ddist[100005], st[100005];
bool yes[100005];
iii ed[3000005];
vector<ii> adj[100005];
priority_queue<iii, vector<iii>, greater<iii> > pq2;
priority_queue<ii, vector<ii>, greater<ii> > pq;
pair<int, ii> shortest_pair() {
int fp = 1e16;
ii fpp;
for (int i = 1; i <= N; i++) {
if (yes[i]) {
dist[i] = 0;
st[i] = i;
pq2.emplace(0, i, i);
} else {
dist[i] = 1e16;
st[i] = -1;
}
}
while (!pq2.empty()) {
auto [d, u, stt] = pq2.top();
pq2.pop();
if (d != dist[u]) continue;
for (auto v : adj[u])
if (dist[v.first] > d + v.second) {
dist[v.first] = d + v.second;
st[v.first] = stt;
pq2.emplace(dist[v.first], v.first, stt);
}
}
for (int i = 1, u, v, w; i <= M; i++) {
tie(u, v, w) = ed[i];
if (st[u] != -1 && st[u] != st[v] && dist[u] + dist[v] + w < fp) {
fp = dist[u] + dist[v] + w;
fpp = mp(st[u], st[v]);
}
}
return mp(fp, fpp);
}
void dijk(int s) {
for (int i = 1; i <= N; i++)
if (i == s) ddist[i] = 0;
else ddist[i] = 1e16;
pq.emplace(0, s);
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d != ddist[u]) continue;
for (auto v : adj[u])
if (ddist[v.first] > d + v.second) {
ddist[v.first] = d + v.second;
pq.emplace(ddist[v.first], v.first);
}
}
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M >> K;
for (int i = 1, u, v, w; i <= M; i++) {
cin >> u >> v >> w;
adj[u].eb(v, w);
adj[v].eb(u, w);
ed[i] = mt(u, v, w);
}
for (int i = 1, x; i <= K; i++) {
cin >> x;
yes[x] = 1;
}
auto f = shortest_pair();
yes[f.second.first] = yes[f.second.second] = 0;
auto s = shortest_pair();
ans = min(ans, f.first + s.first);
yes[f.second.first] = yes[f.second.second] = 1;
dijk(f.second.first);
for (int i = 1; i <= N; i++)
if (yes[i] && i != f.second.first && i != f.second.second) s = min(s, mp(ddist[i], mp(f.second.first, i)));
dijk(f.second.second);
for (int i = 1; i <= N; i++)
if (yes[i] && i != f.second.first && i != f.second.second) s = min(s, mp(ddist[i], mp(f.second.second, i)));
if (f.second.first != s.second.first && f.second.first != s.second.second && f.second.second != s.second.first && f.second.second != s.second.second) ans = min(ans, f.first + s.first);
else {
int fff, sss, common;
yes[s.second.first] = yes[s.second.second] = 0;
auto ff = shortest_pair();
ans = min(ans, ff.first + s.first);
yes[s.second.first] = yes[s.second.second] = 1;
for (int x : {f.second.first, f.second.second})
if (x == s.second.first || x == s.second.second) common = x;
for (int x : {f.second.first, f.second.second})
if (x != common) fff = x;
for (int x : {s.second.first, s.second.second})
if (x != common) sss = x;
yes[fff] = yes[sss] = 0;
auto ffff = shortest_pair();
dijk(fff);
ans = min(ans, ffff.first + ddist[sss]);
yes[fff] = yes[sss] = 1;
}
cout << ans << '\n';
}
Compilation message
RelayMarathon.cpp:86:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
86 | main() {
| ^~~~
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:128:12: warning: 'fff' may be used uninitialized in this function [-Wmaybe-uninitialized]
128 | yes[fff] = yes[sss] = 1;
| ~~~~~~~~~^~~~~~~~~~~~~~
RelayMarathon.cpp:127:40: warning: 'sss' may be used uninitialized in this function [-Wmaybe-uninitialized]
127 | ans = min(ans, ffff.first + ddist[sss]);
| ~~~~~~~~~^
RelayMarathon.cpp:121:4: warning: 'common' may be used uninitialized in this function [-Wmaybe-uninitialized]
121 | if (x != common) fff = x;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2760 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
14 |
Correct |
2 ms |
2788 KB |
Output is correct |
15 |
Correct |
2 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2680 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
2 ms |
2636 KB |
Output is correct |
24 |
Correct |
2 ms |
2636 KB |
Output is correct |
25 |
Correct |
1 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2636 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2760 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
14 |
Correct |
2 ms |
2788 KB |
Output is correct |
15 |
Correct |
2 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2680 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
2 ms |
2636 KB |
Output is correct |
24 |
Correct |
2 ms |
2636 KB |
Output is correct |
25 |
Correct |
1 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2636 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Correct |
2 ms |
2668 KB |
Output is correct |
32 |
Correct |
2 ms |
2764 KB |
Output is correct |
33 |
Correct |
2 ms |
2764 KB |
Output is correct |
34 |
Correct |
2 ms |
2636 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
5 ms |
3020 KB |
Output is correct |
37 |
Correct |
4 ms |
3148 KB |
Output is correct |
38 |
Correct |
2 ms |
2764 KB |
Output is correct |
39 |
Correct |
36 ms |
9324 KB |
Output is correct |
40 |
Correct |
6 ms |
4172 KB |
Output is correct |
41 |
Correct |
3 ms |
2820 KB |
Output is correct |
42 |
Correct |
8 ms |
4220 KB |
Output is correct |
43 |
Correct |
5 ms |
2892 KB |
Output is correct |
44 |
Correct |
2 ms |
2764 KB |
Output is correct |
45 |
Correct |
2 ms |
2636 KB |
Output is correct |
46 |
Correct |
41 ms |
9776 KB |
Output is correct |
47 |
Correct |
10 ms |
3844 KB |
Output is correct |
48 |
Correct |
25 ms |
10016 KB |
Output is correct |
49 |
Correct |
23 ms |
10276 KB |
Output is correct |
50 |
Correct |
3 ms |
2768 KB |
Output is correct |
51 |
Correct |
3 ms |
2816 KB |
Output is correct |
52 |
Correct |
3 ms |
2768 KB |
Output is correct |
53 |
Correct |
19 ms |
6704 KB |
Output is correct |
54 |
Correct |
36 ms |
10440 KB |
Output is correct |
55 |
Correct |
3 ms |
2768 KB |
Output is correct |
56 |
Correct |
2 ms |
2640 KB |
Output is correct |
57 |
Correct |
4 ms |
2688 KB |
Output is correct |
58 |
Correct |
29 ms |
11292 KB |
Output is correct |
59 |
Correct |
3 ms |
2640 KB |
Output is correct |
60 |
Correct |
3 ms |
2820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
10628 KB |
Output is correct |
2 |
Correct |
7 ms |
5720 KB |
Output is correct |
3 |
Correct |
1967 ms |
223084 KB |
Output is correct |
4 |
Correct |
895 ms |
98660 KB |
Output is correct |
5 |
Correct |
213 ms |
19224 KB |
Output is correct |
6 |
Correct |
193 ms |
17936 KB |
Output is correct |
7 |
Correct |
249 ms |
24092 KB |
Output is correct |
8 |
Correct |
98 ms |
12068 KB |
Output is correct |
9 |
Correct |
159 ms |
16796 KB |
Output is correct |
10 |
Correct |
150 ms |
13648 KB |
Output is correct |
11 |
Correct |
1850 ms |
239792 KB |
Output is correct |
12 |
Correct |
127 ms |
14488 KB |
Output is correct |
13 |
Correct |
605 ms |
65620 KB |
Output is correct |
14 |
Correct |
303 ms |
26008 KB |
Output is correct |
15 |
Correct |
1794 ms |
235164 KB |
Output is correct |
16 |
Correct |
58 ms |
10780 KB |
Output is correct |
17 |
Correct |
1285 ms |
162804 KB |
Output is correct |
18 |
Correct |
6 ms |
5884 KB |
Output is correct |
19 |
Correct |
2204 ms |
247268 KB |
Output is correct |
20 |
Correct |
265 ms |
22656 KB |
Output is correct |
21 |
Correct |
229 ms |
20368 KB |
Output is correct |
22 |
Correct |
85 ms |
13236 KB |
Output is correct |
23 |
Correct |
35 ms |
9144 KB |
Output is correct |
24 |
Correct |
1231 ms |
177188 KB |
Output is correct |
25 |
Correct |
174 ms |
16956 KB |
Output is correct |
26 |
Correct |
80 ms |
11980 KB |
Output is correct |
27 |
Correct |
117 ms |
14968 KB |
Output is correct |
28 |
Correct |
17 ms |
7312 KB |
Output is correct |
29 |
Correct |
193 ms |
23352 KB |
Output is correct |
30 |
Correct |
562 ms |
49708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2760 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
14 |
Correct |
2 ms |
2788 KB |
Output is correct |
15 |
Correct |
2 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2680 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
2 ms |
2636 KB |
Output is correct |
24 |
Correct |
2 ms |
2636 KB |
Output is correct |
25 |
Correct |
1 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2636 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Correct |
2 ms |
2668 KB |
Output is correct |
32 |
Correct |
2 ms |
2764 KB |
Output is correct |
33 |
Correct |
2 ms |
2764 KB |
Output is correct |
34 |
Correct |
2 ms |
2636 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
5 ms |
3020 KB |
Output is correct |
37 |
Correct |
4 ms |
3148 KB |
Output is correct |
38 |
Correct |
2 ms |
2764 KB |
Output is correct |
39 |
Correct |
36 ms |
9324 KB |
Output is correct |
40 |
Correct |
6 ms |
4172 KB |
Output is correct |
41 |
Correct |
3 ms |
2820 KB |
Output is correct |
42 |
Correct |
8 ms |
4220 KB |
Output is correct |
43 |
Correct |
5 ms |
2892 KB |
Output is correct |
44 |
Correct |
2 ms |
2764 KB |
Output is correct |
45 |
Correct |
2 ms |
2636 KB |
Output is correct |
46 |
Correct |
41 ms |
9776 KB |
Output is correct |
47 |
Correct |
10 ms |
3844 KB |
Output is correct |
48 |
Correct |
25 ms |
10016 KB |
Output is correct |
49 |
Correct |
23 ms |
10276 KB |
Output is correct |
50 |
Correct |
3 ms |
2768 KB |
Output is correct |
51 |
Correct |
3 ms |
2816 KB |
Output is correct |
52 |
Correct |
3 ms |
2768 KB |
Output is correct |
53 |
Correct |
19 ms |
6704 KB |
Output is correct |
54 |
Correct |
36 ms |
10440 KB |
Output is correct |
55 |
Correct |
3 ms |
2768 KB |
Output is correct |
56 |
Correct |
2 ms |
2640 KB |
Output is correct |
57 |
Correct |
4 ms |
2688 KB |
Output is correct |
58 |
Correct |
29 ms |
11292 KB |
Output is correct |
59 |
Correct |
3 ms |
2640 KB |
Output is correct |
60 |
Correct |
3 ms |
2820 KB |
Output is correct |
61 |
Correct |
85 ms |
10628 KB |
Output is correct |
62 |
Correct |
7 ms |
5720 KB |
Output is correct |
63 |
Correct |
1967 ms |
223084 KB |
Output is correct |
64 |
Correct |
895 ms |
98660 KB |
Output is correct |
65 |
Correct |
213 ms |
19224 KB |
Output is correct |
66 |
Correct |
193 ms |
17936 KB |
Output is correct |
67 |
Correct |
249 ms |
24092 KB |
Output is correct |
68 |
Correct |
98 ms |
12068 KB |
Output is correct |
69 |
Correct |
159 ms |
16796 KB |
Output is correct |
70 |
Correct |
150 ms |
13648 KB |
Output is correct |
71 |
Correct |
1850 ms |
239792 KB |
Output is correct |
72 |
Correct |
127 ms |
14488 KB |
Output is correct |
73 |
Correct |
605 ms |
65620 KB |
Output is correct |
74 |
Correct |
303 ms |
26008 KB |
Output is correct |
75 |
Correct |
1794 ms |
235164 KB |
Output is correct |
76 |
Correct |
58 ms |
10780 KB |
Output is correct |
77 |
Correct |
1285 ms |
162804 KB |
Output is correct |
78 |
Correct |
6 ms |
5884 KB |
Output is correct |
79 |
Correct |
2204 ms |
247268 KB |
Output is correct |
80 |
Correct |
265 ms |
22656 KB |
Output is correct |
81 |
Correct |
229 ms |
20368 KB |
Output is correct |
82 |
Correct |
85 ms |
13236 KB |
Output is correct |
83 |
Correct |
35 ms |
9144 KB |
Output is correct |
84 |
Correct |
1231 ms |
177188 KB |
Output is correct |
85 |
Correct |
174 ms |
16956 KB |
Output is correct |
86 |
Correct |
80 ms |
11980 KB |
Output is correct |
87 |
Correct |
117 ms |
14968 KB |
Output is correct |
88 |
Correct |
17 ms |
7312 KB |
Output is correct |
89 |
Correct |
193 ms |
23352 KB |
Output is correct |
90 |
Correct |
562 ms |
49708 KB |
Output is correct |
91 |
Correct |
300 ms |
16856 KB |
Output is correct |
92 |
Correct |
1928 ms |
258088 KB |
Output is correct |
93 |
Correct |
362 ms |
25960 KB |
Output is correct |
94 |
Correct |
1304 ms |
152084 KB |
Output is correct |
95 |
Correct |
7 ms |
5908 KB |
Output is correct |
96 |
Correct |
9 ms |
6184 KB |
Output is correct |
97 |
Correct |
371 ms |
34664 KB |
Output is correct |
98 |
Correct |
210 ms |
16556 KB |
Output is correct |
99 |
Correct |
167 ms |
17496 KB |
Output is correct |
100 |
Correct |
2188 ms |
278364 KB |
Output is correct |
101 |
Correct |
1039 ms |
102492 KB |
Output is correct |
102 |
Correct |
884 ms |
100888 KB |
Output is correct |
103 |
Correct |
1412 ms |
183456 KB |
Output is correct |
104 |
Correct |
1750 ms |
219060 KB |
Output is correct |
105 |
Correct |
130 ms |
12264 KB |
Output is correct |
106 |
Correct |
1466 ms |
187920 KB |
Output is correct |
107 |
Correct |
513 ms |
53724 KB |
Output is correct |
108 |
Correct |
284 ms |
23388 KB |
Output is correct |
109 |
Correct |
18 ms |
8064 KB |
Output is correct |
110 |
Correct |
95 ms |
11328 KB |
Output is correct |
111 |
Correct |
398 ms |
24200 KB |
Output is correct |
112 |
Correct |
2239 ms |
276868 KB |
Output is correct |
113 |
Correct |
2118 ms |
279120 KB |
Output is correct |
114 |
Correct |
339 ms |
23080 KB |
Output is correct |
115 |
Correct |
860 ms |
93700 KB |
Output is correct |
116 |
Correct |
1884 ms |
253188 KB |
Output is correct |
117 |
Correct |
1284 ms |
179864 KB |
Output is correct |
118 |
Correct |
1294 ms |
162748 KB |
Output is correct |
119 |
Correct |
1882 ms |
227452 KB |
Output is correct |
120 |
Correct |
1213 ms |
134992 KB |
Output is correct |