#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
vector<pair<int, int> > adj[N];
int n, d[N], dx1[N], dy1[N], p[N];
set<int> S;
bool vis[N];
bool skip[N];
vector<pair<pair<int, int>, int> > E;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Q;
void djikstra() {
while(!Q.empty()) {
int u = Q.top().second;
Q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto nbr : adj[u]) {
int v = nbr.first, w = nbr.second;
if(skip[v]) continue;
if(d[u] + w < d[v]) {
d[v] = d[u] + w;
Q.push({d[v], v});
p[v] = p[u];
}
}
}
}
void run(int src) {
while(!Q.empty()) Q.pop();
for(int i = 1; i <= n + 4; i++) d[i] = INT_MAX, vis[i] = 0;
d[src] = 0;
p[src] = src;
Q.push({0, src});
djikstra();
}
pair<pair<int, int>, int> run_nearest() {
while(!Q.empty()) Q.pop();
for(int i = 1; i <= n + 4; i++) d[i] = INT_MAX, vis[i] = 0;
for(auto u : S) {
if(skip[u]) continue;
Q.push({0, u});
d[u] = 0;
p[u] = u;
}
djikstra();
int ans = INT_MAX, a = -1, b = -1;
for(auto e : E) {
int u = e.first.first, v = e.first.second, w = e.second;
if(skip[u] == 1 or skip[v] == 1) continue;
if(d[u] == INT_MAX or d[v] == INT_MAX or p[u] == p[v]) continue;
if(ans > d[u] + w + d[v]) {
ans = d[u] + w + d[v];
a = p[u], b = p[v];
}
}
return {{a, b}, ans};
}
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
srand(time(NULL));
int m, k, u, v, w;
cin>>n>>m>>k;
while(m--) {
cin>>u>>v>>w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
E.push_back({{u, v}, w});
}
for(int i = 1; i <= k; i++) {
cin>>u;
S.insert(u);
}
pair<pair<int, int>, int> tmp = run_nearest();
int x1 = tmp.first.first, y1 = tmp.first.second, d1 = tmp.second;
int c1 = -1, c2 = -1;
run(x1);
for(int i = 1; i <= n; i++) dx1[i] = d[i];
run(y1);
for(int i = 1; i <= n; i++) dy1[i] = d[i];
for(auto c : S) {
if(c == x1 or c == y1) continue;
if(c1 == -1 or dy1[c] <= dy1[c1]) {
c2 = c1;
c1 = c;
} else if(c2 == -1 or dy1[c] < dy1[c2]) {
c2 = c;
}
}
int ans = INT_MAX, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
if(dx1[c1] != INT_MAX and dy1[c2] != INT_MAX) {
if(ans > dx1[c1] + dy1[c2]) {
ans = dx1[c1] + dy1[c2];
a1 = x1, a2 = c1, a3 = y1, a4 = c2;
}
}
for(auto x2 : S) {
if(x2 == x1 or x2 == y1 or x2 == c1) continue;
if(dx1[x2] != INT_MAX and dy1[c1] != INT_MAX) {
if(ans > dx1[x2] + dy1[c1]) {
ans = dx1[x2] + dy1[c1];
a1 = x1, a2 = x2, a3 = y1, a4 = c1;
}
}
}
//Try commenting this portion and then checking if it fails or not.
skip[x1] = skip[y1] = 1;
tmp = run_nearest();
int d2 = tmp.second;
if(d1 != INT_MAX and d2 != INT_MAX) {
if(ans > d1 + d2) {
ans = d1 + d2;
a1 = x1, a2 = y1, a3 = tmp.first.first, a4 = tmp.first.second;
}
}
cout<<ans<<endl;
//cerr<<a1<<" "<<a2<<" "<<a3<<" "<<a4<<endl;
}
Compilation message
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:103:22: warning: variable 'a1' set but not used [-Wunused-but-set-variable]
int ans = INT_MAX, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
^~
RelayMarathon.cpp:103:31: warning: variable 'a2' set but not used [-Wunused-but-set-variable]
int ans = INT_MAX, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
^~
RelayMarathon.cpp:103:40: warning: variable 'a3' set but not used [-Wunused-but-set-variable]
int ans = INT_MAX, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
^~
RelayMarathon.cpp:103:49: warning: variable 'a4' set but not used [-Wunused-but-set-variable]
int ans = INT_MAX, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
18 ms |
23936 KB |
Output is correct |
3 |
Correct |
21 ms |
23808 KB |
Output is correct |
4 |
Correct |
15 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23936 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23936 KB |
Output is correct |
8 |
Correct |
16 ms |
23936 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
20 ms |
23808 KB |
Output is correct |
11 |
Correct |
19 ms |
23808 KB |
Output is correct |
12 |
Correct |
17 ms |
23808 KB |
Output is correct |
13 |
Correct |
15 ms |
23936 KB |
Output is correct |
14 |
Correct |
15 ms |
23936 KB |
Output is correct |
15 |
Correct |
18 ms |
23936 KB |
Output is correct |
16 |
Correct |
15 ms |
23808 KB |
Output is correct |
17 |
Correct |
15 ms |
23936 KB |
Output is correct |
18 |
Correct |
20 ms |
23808 KB |
Output is correct |
19 |
Correct |
15 ms |
23936 KB |
Output is correct |
20 |
Correct |
18 ms |
23936 KB |
Output is correct |
21 |
Correct |
23 ms |
23936 KB |
Output is correct |
22 |
Correct |
15 ms |
23808 KB |
Output is correct |
23 |
Correct |
17 ms |
23936 KB |
Output is correct |
24 |
Correct |
19 ms |
23808 KB |
Output is correct |
25 |
Correct |
17 ms |
23808 KB |
Output is correct |
26 |
Correct |
15 ms |
23808 KB |
Output is correct |
27 |
Correct |
17 ms |
23808 KB |
Output is correct |
28 |
Correct |
15 ms |
23936 KB |
Output is correct |
29 |
Correct |
22 ms |
23936 KB |
Output is correct |
30 |
Correct |
15 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
18 ms |
23936 KB |
Output is correct |
3 |
Correct |
21 ms |
23808 KB |
Output is correct |
4 |
Correct |
15 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23936 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23936 KB |
Output is correct |
8 |
Correct |
16 ms |
23936 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
20 ms |
23808 KB |
Output is correct |
11 |
Correct |
19 ms |
23808 KB |
Output is correct |
12 |
Correct |
17 ms |
23808 KB |
Output is correct |
13 |
Correct |
15 ms |
23936 KB |
Output is correct |
14 |
Correct |
15 ms |
23936 KB |
Output is correct |
15 |
Correct |
18 ms |
23936 KB |
Output is correct |
16 |
Correct |
15 ms |
23808 KB |
Output is correct |
17 |
Correct |
15 ms |
23936 KB |
Output is correct |
18 |
Correct |
20 ms |
23808 KB |
Output is correct |
19 |
Correct |
15 ms |
23936 KB |
Output is correct |
20 |
Correct |
18 ms |
23936 KB |
Output is correct |
21 |
Correct |
23 ms |
23936 KB |
Output is correct |
22 |
Correct |
15 ms |
23808 KB |
Output is correct |
23 |
Correct |
17 ms |
23936 KB |
Output is correct |
24 |
Correct |
19 ms |
23808 KB |
Output is correct |
25 |
Correct |
17 ms |
23808 KB |
Output is correct |
26 |
Correct |
15 ms |
23808 KB |
Output is correct |
27 |
Correct |
17 ms |
23808 KB |
Output is correct |
28 |
Correct |
15 ms |
23936 KB |
Output is correct |
29 |
Correct |
22 ms |
23936 KB |
Output is correct |
30 |
Correct |
15 ms |
23936 KB |
Output is correct |
31 |
Correct |
15 ms |
23936 KB |
Output is correct |
32 |
Correct |
15 ms |
23936 KB |
Output is correct |
33 |
Correct |
24 ms |
23936 KB |
Output is correct |
34 |
Correct |
15 ms |
23936 KB |
Output is correct |
35 |
Correct |
23 ms |
23808 KB |
Output is correct |
36 |
Correct |
25 ms |
24064 KB |
Output is correct |
37 |
Correct |
26 ms |
24192 KB |
Output is correct |
38 |
Correct |
16 ms |
23936 KB |
Output is correct |
39 |
Correct |
43 ms |
27316 KB |
Output is correct |
40 |
Correct |
24 ms |
24704 KB |
Output is correct |
41 |
Correct |
20 ms |
23936 KB |
Output is correct |
42 |
Correct |
29 ms |
24704 KB |
Output is correct |
43 |
Correct |
21 ms |
24064 KB |
Output is correct |
44 |
Correct |
15 ms |
23936 KB |
Output is correct |
45 |
Correct |
23 ms |
23936 KB |
Output is correct |
46 |
Correct |
44 ms |
27320 KB |
Output is correct |
47 |
Correct |
20 ms |
24320 KB |
Output is correct |
48 |
Correct |
40 ms |
27320 KB |
Output is correct |
49 |
Correct |
69 ms |
27312 KB |
Output is correct |
50 |
Correct |
17 ms |
23936 KB |
Output is correct |
51 |
Correct |
15 ms |
23936 KB |
Output is correct |
52 |
Correct |
19 ms |
23936 KB |
Output is correct |
53 |
Correct |
29 ms |
25532 KB |
Output is correct |
54 |
Correct |
63 ms |
27312 KB |
Output is correct |
55 |
Correct |
20 ms |
23936 KB |
Output is correct |
56 |
Correct |
16 ms |
23840 KB |
Output is correct |
57 |
Correct |
17 ms |
23912 KB |
Output is correct |
58 |
Correct |
49 ms |
27436 KB |
Output is correct |
59 |
Correct |
17 ms |
23808 KB |
Output is correct |
60 |
Correct |
24 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
29100 KB |
Output is correct |
2 |
Correct |
32 ms |
25724 KB |
Output is correct |
3 |
Correct |
2090 ms |
128376 KB |
Output is correct |
4 |
Correct |
1064 ms |
66184 KB |
Output is correct |
5 |
Correct |
209 ms |
33952 KB |
Output is correct |
6 |
Correct |
285 ms |
32028 KB |
Output is correct |
7 |
Correct |
241 ms |
34872 KB |
Output is correct |
8 |
Correct |
103 ms |
29224 KB |
Output is correct |
9 |
Correct |
170 ms |
31528 KB |
Output is correct |
10 |
Correct |
150 ms |
29992 KB |
Output is correct |
11 |
Correct |
2496 ms |
128376 KB |
Output is correct |
12 |
Correct |
166 ms |
30372 KB |
Output is correct |
13 |
Correct |
974 ms |
53988 KB |
Output is correct |
14 |
Correct |
366 ms |
35240 KB |
Output is correct |
15 |
Correct |
2279 ms |
128396 KB |
Output is correct |
16 |
Correct |
83 ms |
28720 KB |
Output is correct |
17 |
Correct |
1588 ms |
90764 KB |
Output is correct |
18 |
Correct |
25 ms |
25976 KB |
Output is correct |
19 |
Correct |
2663 ms |
128408 KB |
Output is correct |
20 |
Correct |
276 ms |
34736 KB |
Output is correct |
21 |
Correct |
264 ms |
33340 KB |
Output is correct |
22 |
Correct |
139 ms |
29868 KB |
Output is correct |
23 |
Correct |
42 ms |
27952 KB |
Output is correct |
24 |
Correct |
1955 ms |
98796 KB |
Output is correct |
25 |
Correct |
213 ms |
31656 KB |
Output is correct |
26 |
Correct |
100 ms |
29356 KB |
Output is correct |
27 |
Correct |
153 ms |
30760 KB |
Output is correct |
28 |
Correct |
49 ms |
26936 KB |
Output is correct |
29 |
Correct |
224 ms |
34488 KB |
Output is correct |
30 |
Correct |
567 ms |
44296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
18 ms |
23936 KB |
Output is correct |
3 |
Correct |
21 ms |
23808 KB |
Output is correct |
4 |
Correct |
15 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23936 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23936 KB |
Output is correct |
8 |
Correct |
16 ms |
23936 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
20 ms |
23808 KB |
Output is correct |
11 |
Correct |
19 ms |
23808 KB |
Output is correct |
12 |
Correct |
17 ms |
23808 KB |
Output is correct |
13 |
Correct |
15 ms |
23936 KB |
Output is correct |
14 |
Correct |
15 ms |
23936 KB |
Output is correct |
15 |
Correct |
18 ms |
23936 KB |
Output is correct |
16 |
Correct |
15 ms |
23808 KB |
Output is correct |
17 |
Correct |
15 ms |
23936 KB |
Output is correct |
18 |
Correct |
20 ms |
23808 KB |
Output is correct |
19 |
Correct |
15 ms |
23936 KB |
Output is correct |
20 |
Correct |
18 ms |
23936 KB |
Output is correct |
21 |
Correct |
23 ms |
23936 KB |
Output is correct |
22 |
Correct |
15 ms |
23808 KB |
Output is correct |
23 |
Correct |
17 ms |
23936 KB |
Output is correct |
24 |
Correct |
19 ms |
23808 KB |
Output is correct |
25 |
Correct |
17 ms |
23808 KB |
Output is correct |
26 |
Correct |
15 ms |
23808 KB |
Output is correct |
27 |
Correct |
17 ms |
23808 KB |
Output is correct |
28 |
Correct |
15 ms |
23936 KB |
Output is correct |
29 |
Correct |
22 ms |
23936 KB |
Output is correct |
30 |
Correct |
15 ms |
23936 KB |
Output is correct |
31 |
Correct |
15 ms |
23936 KB |
Output is correct |
32 |
Correct |
15 ms |
23936 KB |
Output is correct |
33 |
Correct |
24 ms |
23936 KB |
Output is correct |
34 |
Correct |
15 ms |
23936 KB |
Output is correct |
35 |
Correct |
23 ms |
23808 KB |
Output is correct |
36 |
Correct |
25 ms |
24064 KB |
Output is correct |
37 |
Correct |
26 ms |
24192 KB |
Output is correct |
38 |
Correct |
16 ms |
23936 KB |
Output is correct |
39 |
Correct |
43 ms |
27316 KB |
Output is correct |
40 |
Correct |
24 ms |
24704 KB |
Output is correct |
41 |
Correct |
20 ms |
23936 KB |
Output is correct |
42 |
Correct |
29 ms |
24704 KB |
Output is correct |
43 |
Correct |
21 ms |
24064 KB |
Output is correct |
44 |
Correct |
15 ms |
23936 KB |
Output is correct |
45 |
Correct |
23 ms |
23936 KB |
Output is correct |
46 |
Correct |
44 ms |
27320 KB |
Output is correct |
47 |
Correct |
20 ms |
24320 KB |
Output is correct |
48 |
Correct |
40 ms |
27320 KB |
Output is correct |
49 |
Correct |
69 ms |
27312 KB |
Output is correct |
50 |
Correct |
17 ms |
23936 KB |
Output is correct |
51 |
Correct |
15 ms |
23936 KB |
Output is correct |
52 |
Correct |
19 ms |
23936 KB |
Output is correct |
53 |
Correct |
29 ms |
25532 KB |
Output is correct |
54 |
Correct |
63 ms |
27312 KB |
Output is correct |
55 |
Correct |
20 ms |
23936 KB |
Output is correct |
56 |
Correct |
16 ms |
23840 KB |
Output is correct |
57 |
Correct |
17 ms |
23912 KB |
Output is correct |
58 |
Correct |
49 ms |
27436 KB |
Output is correct |
59 |
Correct |
17 ms |
23808 KB |
Output is correct |
60 |
Correct |
24 ms |
23936 KB |
Output is correct |
61 |
Correct |
145 ms |
29100 KB |
Output is correct |
62 |
Correct |
32 ms |
25724 KB |
Output is correct |
63 |
Correct |
2090 ms |
128376 KB |
Output is correct |
64 |
Correct |
1064 ms |
66184 KB |
Output is correct |
65 |
Correct |
209 ms |
33952 KB |
Output is correct |
66 |
Correct |
285 ms |
32028 KB |
Output is correct |
67 |
Correct |
241 ms |
34872 KB |
Output is correct |
68 |
Correct |
103 ms |
29224 KB |
Output is correct |
69 |
Correct |
170 ms |
31528 KB |
Output is correct |
70 |
Correct |
150 ms |
29992 KB |
Output is correct |
71 |
Correct |
2496 ms |
128376 KB |
Output is correct |
72 |
Correct |
166 ms |
30372 KB |
Output is correct |
73 |
Correct |
974 ms |
53988 KB |
Output is correct |
74 |
Correct |
366 ms |
35240 KB |
Output is correct |
75 |
Correct |
2279 ms |
128396 KB |
Output is correct |
76 |
Correct |
83 ms |
28720 KB |
Output is correct |
77 |
Correct |
1588 ms |
90764 KB |
Output is correct |
78 |
Correct |
25 ms |
25976 KB |
Output is correct |
79 |
Correct |
2663 ms |
128408 KB |
Output is correct |
80 |
Correct |
276 ms |
34736 KB |
Output is correct |
81 |
Correct |
264 ms |
33340 KB |
Output is correct |
82 |
Correct |
139 ms |
29868 KB |
Output is correct |
83 |
Correct |
42 ms |
27952 KB |
Output is correct |
84 |
Correct |
1955 ms |
98796 KB |
Output is correct |
85 |
Correct |
213 ms |
31656 KB |
Output is correct |
86 |
Correct |
100 ms |
29356 KB |
Output is correct |
87 |
Correct |
153 ms |
30760 KB |
Output is correct |
88 |
Correct |
49 ms |
26936 KB |
Output is correct |
89 |
Correct |
224 ms |
34488 KB |
Output is correct |
90 |
Correct |
567 ms |
44296 KB |
Output is correct |
91 |
Correct |
265 ms |
31196 KB |
Output is correct |
92 |
Correct |
2993 ms |
130360 KB |
Output is correct |
93 |
Correct |
376 ms |
34864 KB |
Output is correct |
94 |
Correct |
1614 ms |
85996 KB |
Output is correct |
95 |
Correct |
23 ms |
25848 KB |
Output is correct |
96 |
Correct |
24 ms |
26112 KB |
Output is correct |
97 |
Correct |
395 ms |
39448 KB |
Output is correct |
98 |
Correct |
195 ms |
31144 KB |
Output is correct |
99 |
Correct |
159 ms |
31788 KB |
Output is correct |
100 |
Correct |
2598 ms |
138312 KB |
Output is correct |
101 |
Correct |
1091 ms |
65800 KB |
Output is correct |
102 |
Correct |
1033 ms |
65300 KB |
Output is correct |
103 |
Correct |
1735 ms |
101448 KB |
Output is correct |
104 |
Correct |
1999 ms |
128356 KB |
Output is correct |
105 |
Correct |
149 ms |
29352 KB |
Output is correct |
106 |
Correct |
1718 ms |
102224 KB |
Output is correct |
107 |
Correct |
660 ms |
45872 KB |
Output is correct |
108 |
Correct |
362 ms |
33928 KB |
Output is correct |
109 |
Correct |
40 ms |
27324 KB |
Output is correct |
110 |
Correct |
148 ms |
29224 KB |
Output is correct |
111 |
Correct |
340 ms |
34212 KB |
Output is correct |
112 |
Correct |
2610 ms |
138424 KB |
Output is correct |
113 |
Correct |
2316 ms |
139204 KB |
Output is correct |
114 |
Correct |
269 ms |
33824 KB |
Output is correct |
115 |
Correct |
934 ms |
64932 KB |
Output is correct |
116 |
Correct |
2089 ms |
128364 KB |
Output is correct |
117 |
Correct |
1458 ms |
99948 KB |
Output is correct |
118 |
Correct |
1341 ms |
91372 KB |
Output is correct |
119 |
Correct |
1927 ms |
128580 KB |
Output is correct |
120 |
Correct |
1388 ms |
78852 KB |
Output is correct |