#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (100005)
ll dist[MAXN], distA[MAXN], distB[MAXN];
ll source[MAXN];
vector<pair<ll,ll> > v[MAXN];
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,M,K;
cin>>N>>M>>K;
vector<pair<ll,pair<ll,ll> > > edges;
for(ll i = 0;i < M;i++){
ll a,b,c;
cin>>a>>b>>c;
a--;
b--;
v[a].push_back({c,b});
v[b].push_back({c,a});
edges.push_back({c,{a,b}});
}
priority_queue<pair<ll,pair<ll,ll> >,vector<pair<ll,pair<ll,ll> > >,greater<pair<ll,pair<ll,ll> > > > pq;
ll special[K];
memset(dist,-1,sizeof(dist));
memset(source,-1,sizeof(source));
for(ll i = 0;i < K;i++){
cin>>special[i];
special[i]--;
pq.push({0,{special[i],special[i]}}); //d, cur node, source
dist[special[i]] = 0;
source[special[i]] = special[i];
}
while(!pq.empty()){
ll d = pq.top().first;
ll x = pq.top().second.first;
ll Source = pq.top().second.second;
pq.pop();
if(d != dist[x]){
continue;
}
for(auto u : v[x]){
if(dist[u.second] == -1 || d + u.first < dist[u.second]){
pq.push({d + u.first,{u.second,Source}});
dist[u.second] = d + u.first;
source[u.second] = Source;
}
}
}
ll A = -1, B = -1, minimum = 1e18;
for(auto u : edges){
ll a = u.second.first;
ll b = u.second.second;
ll c = u.first;
if(dist[a] == -1 || dist[b] == -1 || source[a] == source[b]){
continue;
}
if(dist[a] + c + dist[b] <= minimum){
minimum = dist[a] + c + dist[b];
A = source[a];
B = source[b];
}
}
ll sum = 1e18;
//Case 1: First Pair (A, B) fixed, Second pair (C, D) unknown
memset(dist,-1,sizeof(dist));
memset(source,-1,sizeof(source));
for(ll i = 0;i < K;i++){
if(special[i] != A && special[i] != B){
//cout<<"SOURCE: "<<special[i]<<'\n';
pq.push({0,{special[i],special[i]}}); //d, cur node, source
dist[special[i]] = 0;
source[special[i]] = special[i];
}
}
while(!pq.empty()){
ll d = pq.top().first;
ll x = pq.top().second.first;
ll Source = pq.top().second.second;
pq.pop();
if(x == A || x == B) continue;
if(d != dist[x]){
continue;
}
for(auto u : v[x]){
if(u.second == A || u.second == B) continue;
if(dist[u.second] == -1 || d + u.first < dist[u.second]){
pq.push({d + u.first,{u.second,Source}});
dist[u.second] = d + u.first;
source[u.second] = Source;
}
}
}
ll C = -1, D = -1, minimum2 = 1e18;
for(auto u : edges){
ll a = u.second.first;
ll b = u.second.second;
ll c = u.first;
if(dist[a] == -1 || dist[b] == -1 || source[a] == source[b]){
continue;
}
if(dist[a] + c + dist[b] <= minimum2){
minimum2 = dist[a] + c + dist[b];
C = source[a];
D = source[b];
}
}
sum = min(sum,minimum + minimum2);
//cout<<"Case 1 SUM: "<<minimum + minimum2<<" "<<A<<" with "<<B<<" , "<<C<<" with "<<D<<'\n';
//Case 2: Half of both pairs are known (A, C) and (B, D)
memset(distA,-1,sizeof(distA));
pq.push({0,{A,A}}); //d, cur node, source
distA[A] = 0;
source[A] = A;
while(!pq.empty()){ //dikjstra from A
ll d = pq.top().first;
ll x = pq.top().second.first;
ll Source = pq.top().second.second;
pq.pop();
if(d != distA[x]){
continue;
}
for(auto u : v[x]){
if(distA[u.second] == -1 || d + u.first < distA[u.second]){
pq.push({d + u.first,{u.second,Source}});
distA[u.second] = d + u.first;
source[u.second] = Source;
}
}
}
pair<ll,ll> C1 = {-1,1e18}, C2 = {-1,1e18}; //first-best choice for C, second-best choice for C
for(ll i = 0;i < K;i++){
ll x = special[i];
if(distA[x] == -1 || x == A || x == B) continue;
if(distA[x] <= C1.second){
C2 = C1;
C1 = {x,distA[x]};
}else if(distA[x] <= C2.second){
C2 = {x,distA[x]};
}
}
memset(distB,-1,sizeof(distB));
pq.push({0,{B,B}}); //d, cur node, source
distB[B] = 0;
source[B] = B;
while(!pq.empty()){ //dikjstra from B
ll d = pq.top().first;
ll x = pq.top().second.first;
ll Source = pq.top().second.second;
pq.pop();
if(d != distB[x]){
continue;
}
for(auto u : v[x]){
if(distB[u.second] == -1 || d + u.first < distB[u.second]){
pq.push({d + u.first,{u.second,Source}});
distB[u.second] = d + u.first;
source[u.second] = Source;
}
}
}
for(ll i = 0;i < K;i++){
ll D = special[i];
if(D == A || D == B || C1.first == -1 || distB[D] == -1) continue;
if(D == C1.first){
if(C2.first == -1) continue;
sum = min(sum,C2.second + distB[D]);
//cout<<"Case 2 SUM: "<<C2.second + distB[D]<<" "<<A<<" with "<<C2.first<<" , "<<B<<" with "<<D<<'\n';
}else{
sum = min(sum,C1.second + distB[D]);
//cout<<"Case 2 SUM: "<<C1.second + distB[D]<<" "<<A<<" with "<<C1.first<<" , "<<B<<" with "<<D<<'\n';
}
}
cout<<sum<<'\n';
}
Compilation message
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:94:5: warning: variable 'C' set but not used [-Wunused-but-set-variable]
94 | ll C = -1, D = -1, minimum2 = 1e18;
| ^
RelayMarathon.cpp:94:13: warning: variable 'D' set but not used [-Wunused-but-set-variable]
94 | ll C = -1, D = -1, minimum2 = 1e18;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5784 KB |
Output is correct |
7 |
Correct |
2 ms |
5976 KB |
Output is correct |
8 |
Correct |
2 ms |
6232 KB |
Output is correct |
9 |
Correct |
1 ms |
5764 KB |
Output is correct |
10 |
Correct |
1 ms |
5720 KB |
Output is correct |
11 |
Correct |
2 ms |
5724 KB |
Output is correct |
12 |
Correct |
2 ms |
5720 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
6232 KB |
Output is correct |
16 |
Correct |
1 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5976 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
19 |
Correct |
2 ms |
6028 KB |
Output is correct |
20 |
Correct |
2 ms |
5764 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
22 |
Correct |
1 ms |
5728 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
1 ms |
5724 KB |
Output is correct |
25 |
Correct |
1 ms |
5724 KB |
Output is correct |
26 |
Correct |
2 ms |
5724 KB |
Output is correct |
27 |
Correct |
2 ms |
5764 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
29 |
Correct |
2 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5784 KB |
Output is correct |
7 |
Correct |
2 ms |
5976 KB |
Output is correct |
8 |
Correct |
2 ms |
6232 KB |
Output is correct |
9 |
Correct |
1 ms |
5764 KB |
Output is correct |
10 |
Correct |
1 ms |
5720 KB |
Output is correct |
11 |
Correct |
2 ms |
5724 KB |
Output is correct |
12 |
Correct |
2 ms |
5720 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
6232 KB |
Output is correct |
16 |
Correct |
1 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5976 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
19 |
Correct |
2 ms |
6028 KB |
Output is correct |
20 |
Correct |
2 ms |
5764 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
22 |
Correct |
1 ms |
5728 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
1 ms |
5724 KB |
Output is correct |
25 |
Correct |
1 ms |
5724 KB |
Output is correct |
26 |
Correct |
2 ms |
5724 KB |
Output is correct |
27 |
Correct |
2 ms |
5764 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
29 |
Correct |
2 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5980 KB |
Output is correct |
31 |
Correct |
2 ms |
5724 KB |
Output is correct |
32 |
Correct |
2 ms |
5980 KB |
Output is correct |
33 |
Correct |
2 ms |
5980 KB |
Output is correct |
34 |
Correct |
1 ms |
5724 KB |
Output is correct |
35 |
Correct |
1 ms |
5724 KB |
Output is correct |
36 |
Correct |
3 ms |
6260 KB |
Output is correct |
37 |
Correct |
5 ms |
6492 KB |
Output is correct |
38 |
Correct |
2 ms |
5976 KB |
Output is correct |
39 |
Correct |
21 ms |
13340 KB |
Output is correct |
40 |
Correct |
7 ms |
7444 KB |
Output is correct |
41 |
Correct |
2 ms |
5980 KB |
Output is correct |
42 |
Correct |
6 ms |
7700 KB |
Output is correct |
43 |
Correct |
3 ms |
6232 KB |
Output is correct |
44 |
Correct |
2 ms |
5980 KB |
Output is correct |
45 |
Correct |
2 ms |
5724 KB |
Output is correct |
46 |
Correct |
25 ms |
14192 KB |
Output is correct |
47 |
Correct |
5 ms |
7064 KB |
Output is correct |
48 |
Correct |
20 ms |
13236 KB |
Output is correct |
49 |
Correct |
22 ms |
13340 KB |
Output is correct |
50 |
Correct |
2 ms |
5976 KB |
Output is correct |
51 |
Correct |
2 ms |
5976 KB |
Output is correct |
52 |
Correct |
2 ms |
5980 KB |
Output is correct |
53 |
Correct |
13 ms |
9796 KB |
Output is correct |
54 |
Correct |
23 ms |
13572 KB |
Output is correct |
55 |
Correct |
2 ms |
5980 KB |
Output is correct |
56 |
Correct |
2 ms |
5724 KB |
Output is correct |
57 |
Correct |
2 ms |
5980 KB |
Output is correct |
58 |
Correct |
29 ms |
14632 KB |
Output is correct |
59 |
Correct |
2 ms |
5724 KB |
Output is correct |
60 |
Correct |
2 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
13056 KB |
Output is correct |
2 |
Correct |
4 ms |
6488 KB |
Output is correct |
3 |
Correct |
1388 ms |
245748 KB |
Output is correct |
4 |
Correct |
701 ms |
105140 KB |
Output is correct |
5 |
Correct |
127 ms |
25588 KB |
Output is correct |
6 |
Correct |
95 ms |
19708 KB |
Output is correct |
7 |
Correct |
190 ms |
26204 KB |
Output is correct |
8 |
Correct |
45 ms |
12732 KB |
Output is correct |
9 |
Correct |
98 ms |
18092 KB |
Output is correct |
10 |
Correct |
61 ms |
14532 KB |
Output is correct |
11 |
Correct |
1474 ms |
244908 KB |
Output is correct |
12 |
Correct |
88 ms |
15616 KB |
Output is correct |
13 |
Correct |
482 ms |
70104 KB |
Output is correct |
14 |
Correct |
140 ms |
27976 KB |
Output is correct |
15 |
Correct |
1460 ms |
244976 KB |
Output is correct |
16 |
Correct |
31 ms |
11528 KB |
Output is correct |
17 |
Correct |
1086 ms |
164276 KB |
Output is correct |
18 |
Correct |
4 ms |
6808 KB |
Output is correct |
19 |
Correct |
1617 ms |
249380 KB |
Output is correct |
20 |
Correct |
191 ms |
26604 KB |
Output is correct |
21 |
Correct |
118 ms |
24492 KB |
Output is correct |
22 |
Correct |
53 ms |
15104 KB |
Output is correct |
23 |
Correct |
16 ms |
9728 KB |
Output is correct |
24 |
Correct |
1070 ms |
183564 KB |
Output is correct |
25 |
Correct |
85 ms |
18276 KB |
Output is correct |
26 |
Correct |
44 ms |
13324 KB |
Output is correct |
27 |
Correct |
67 ms |
16376 KB |
Output is correct |
28 |
Correct |
11 ms |
8208 KB |
Output is correct |
29 |
Correct |
134 ms |
26868 KB |
Output is correct |
30 |
Correct |
279 ms |
50164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5784 KB |
Output is correct |
7 |
Correct |
2 ms |
5976 KB |
Output is correct |
8 |
Correct |
2 ms |
6232 KB |
Output is correct |
9 |
Correct |
1 ms |
5764 KB |
Output is correct |
10 |
Correct |
1 ms |
5720 KB |
Output is correct |
11 |
Correct |
2 ms |
5724 KB |
Output is correct |
12 |
Correct |
2 ms |
5720 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
6232 KB |
Output is correct |
16 |
Correct |
1 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5976 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
19 |
Correct |
2 ms |
6028 KB |
Output is correct |
20 |
Correct |
2 ms |
5764 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
22 |
Correct |
1 ms |
5728 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
1 ms |
5724 KB |
Output is correct |
25 |
Correct |
1 ms |
5724 KB |
Output is correct |
26 |
Correct |
2 ms |
5724 KB |
Output is correct |
27 |
Correct |
2 ms |
5764 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
29 |
Correct |
2 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5980 KB |
Output is correct |
31 |
Correct |
2 ms |
5724 KB |
Output is correct |
32 |
Correct |
2 ms |
5980 KB |
Output is correct |
33 |
Correct |
2 ms |
5980 KB |
Output is correct |
34 |
Correct |
1 ms |
5724 KB |
Output is correct |
35 |
Correct |
1 ms |
5724 KB |
Output is correct |
36 |
Correct |
3 ms |
6260 KB |
Output is correct |
37 |
Correct |
5 ms |
6492 KB |
Output is correct |
38 |
Correct |
2 ms |
5976 KB |
Output is correct |
39 |
Correct |
21 ms |
13340 KB |
Output is correct |
40 |
Correct |
7 ms |
7444 KB |
Output is correct |
41 |
Correct |
2 ms |
5980 KB |
Output is correct |
42 |
Correct |
6 ms |
7700 KB |
Output is correct |
43 |
Correct |
3 ms |
6232 KB |
Output is correct |
44 |
Correct |
2 ms |
5980 KB |
Output is correct |
45 |
Correct |
2 ms |
5724 KB |
Output is correct |
46 |
Correct |
25 ms |
14192 KB |
Output is correct |
47 |
Correct |
5 ms |
7064 KB |
Output is correct |
48 |
Correct |
20 ms |
13236 KB |
Output is correct |
49 |
Correct |
22 ms |
13340 KB |
Output is correct |
50 |
Correct |
2 ms |
5976 KB |
Output is correct |
51 |
Correct |
2 ms |
5976 KB |
Output is correct |
52 |
Correct |
2 ms |
5980 KB |
Output is correct |
53 |
Correct |
13 ms |
9796 KB |
Output is correct |
54 |
Correct |
23 ms |
13572 KB |
Output is correct |
55 |
Correct |
2 ms |
5980 KB |
Output is correct |
56 |
Correct |
2 ms |
5724 KB |
Output is correct |
57 |
Correct |
2 ms |
5980 KB |
Output is correct |
58 |
Correct |
29 ms |
14632 KB |
Output is correct |
59 |
Correct |
2 ms |
5724 KB |
Output is correct |
60 |
Correct |
2 ms |
5980 KB |
Output is correct |
61 |
Correct |
41 ms |
13056 KB |
Output is correct |
62 |
Correct |
4 ms |
6488 KB |
Output is correct |
63 |
Correct |
1388 ms |
245748 KB |
Output is correct |
64 |
Correct |
701 ms |
105140 KB |
Output is correct |
65 |
Correct |
127 ms |
25588 KB |
Output is correct |
66 |
Correct |
95 ms |
19708 KB |
Output is correct |
67 |
Correct |
190 ms |
26204 KB |
Output is correct |
68 |
Correct |
45 ms |
12732 KB |
Output is correct |
69 |
Correct |
98 ms |
18092 KB |
Output is correct |
70 |
Correct |
61 ms |
14532 KB |
Output is correct |
71 |
Correct |
1474 ms |
244908 KB |
Output is correct |
72 |
Correct |
88 ms |
15616 KB |
Output is correct |
73 |
Correct |
482 ms |
70104 KB |
Output is correct |
74 |
Correct |
140 ms |
27976 KB |
Output is correct |
75 |
Correct |
1460 ms |
244976 KB |
Output is correct |
76 |
Correct |
31 ms |
11528 KB |
Output is correct |
77 |
Correct |
1086 ms |
164276 KB |
Output is correct |
78 |
Correct |
4 ms |
6808 KB |
Output is correct |
79 |
Correct |
1617 ms |
249380 KB |
Output is correct |
80 |
Correct |
191 ms |
26604 KB |
Output is correct |
81 |
Correct |
118 ms |
24492 KB |
Output is correct |
82 |
Correct |
53 ms |
15104 KB |
Output is correct |
83 |
Correct |
16 ms |
9728 KB |
Output is correct |
84 |
Correct |
1070 ms |
183564 KB |
Output is correct |
85 |
Correct |
85 ms |
18276 KB |
Output is correct |
86 |
Correct |
44 ms |
13324 KB |
Output is correct |
87 |
Correct |
67 ms |
16376 KB |
Output is correct |
88 |
Correct |
11 ms |
8208 KB |
Output is correct |
89 |
Correct |
134 ms |
26868 KB |
Output is correct |
90 |
Correct |
279 ms |
50164 KB |
Output is correct |
91 |
Correct |
137 ms |
17452 KB |
Output is correct |
92 |
Correct |
1569 ms |
265008 KB |
Output is correct |
93 |
Correct |
208 ms |
27992 KB |
Output is correct |
94 |
Correct |
976 ms |
152616 KB |
Output is correct |
95 |
Correct |
5 ms |
6808 KB |
Output is correct |
96 |
Correct |
5 ms |
6856 KB |
Output is correct |
97 |
Correct |
306 ms |
36724 KB |
Output is correct |
98 |
Correct |
108 ms |
17148 KB |
Output is correct |
99 |
Correct |
100 ms |
18996 KB |
Output is correct |
100 |
Correct |
1663 ms |
282592 KB |
Output is correct |
101 |
Correct |
726 ms |
106036 KB |
Output is correct |
102 |
Correct |
703 ms |
103904 KB |
Output is correct |
103 |
Correct |
1087 ms |
189520 KB |
Output is correct |
104 |
Correct |
1343 ms |
244660 KB |
Output is correct |
105 |
Correct |
62 ms |
13564 KB |
Output is correct |
106 |
Correct |
1121 ms |
191224 KB |
Output is correct |
107 |
Correct |
400 ms |
53892 KB |
Output is correct |
108 |
Correct |
180 ms |
24720 KB |
Output is correct |
109 |
Correct |
11 ms |
8976 KB |
Output is correct |
110 |
Correct |
53 ms |
13328 KB |
Output is correct |
111 |
Correct |
198 ms |
25336 KB |
Output is correct |
112 |
Correct |
1646 ms |
281912 KB |
Output is correct |
113 |
Correct |
1620 ms |
285320 KB |
Output is correct |
114 |
Correct |
180 ms |
24692 KB |
Output is correct |
115 |
Correct |
585 ms |
99580 KB |
Output is correct |
116 |
Correct |
1460 ms |
260064 KB |
Output is correct |
117 |
Correct |
1075 ms |
184776 KB |
Output is correct |
118 |
Correct |
964 ms |
166536 KB |
Output is correct |
119 |
Correct |
1497 ms |
244676 KB |
Output is correct |
120 |
Correct |
884 ms |
136408 KB |
Output is correct |