#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
const int maxn = 100010;
typedef pair<int,pi> pp;
#define p(x,y,z) pp(x,pi(y,z))
int dist[maxn],col[maxn];
int special[maxn];
int n,m,k;
vector <int> specials;
vector <pi> adjlist[maxn];
int A = -1, B = -1;
int test(int excludea, int excludeb) {
memset(dist,-1,sizeof dist);
memset(col,-1,sizeof col);
priority_queue <pi,vector<pi>,greater<pi>> pq;
for (auto cur: specials) {
if (cur == excludea or cur == excludeb) continue;
pq.push(pi(0,cur));
dist[cur] = 0;
col[cur] = cur;
}
while (!pq.empty()) {
pi cur = pq.top(); pq.pop();
int x = cur.s, d = cur.f;
//cout << "GO: " << x << " " << d << "\n";
if (dist[x] != d) continue;
for (auto i: adjlist[x]) {
if (dist[i.f] == -1 or dist[i.f] > d + i.s) {
dist[i.f] = d + i.s;
col[i.f] = col[x];
pq.push(pi(dist[i.f],i.f));
}
}
}
//cout << "NEW: " << excludea << " " << excludeb << "\n";
//for (int i =1;i<=n;i++) {
//cout << i << " " << dist[i] << " " << col[i] << "\n";
//}
pp ans = p(LLONG_MAX/2,0,0);
for (int i = 1; i <= n;i++) {
for (auto u: adjlist[i]) {
if (col[i] != col[u.f] and col[i] != -1 and col[u.f] != -1) {
if (dist[u.f] + dist[i] + u.s < ans.f) {
ans = p(dist[u.f] + dist[i] + u.s, col[u.f], col[i]);
}
}
}
}
//cout << "ANS: " << ans.f << " " << ans.s.f << " " << ans.s.s << "\n";
if (excludea == -1 or excludeb == -1) {
if (A == -1 and B == -1) {
A = ans.s.f; B = ans.s.s;
}
return ans.f + test(ans.s.f, ans.s.s);
} else return ans.f;
}
int32_t main() {
FAST
cin >> n >> m >> k;
for (int i =0;i<m;i++) {
int a,b,c; cin >> a >> b >> c;
adjlist[a].push_back(pi(b,c));
adjlist[b].push_back(pi(a,c));
}
for (int i =0;i<k;i++) {
int x; cin >> x;
special[x] = 1;
specials.push_back(x);
}
int rans = LLONG_MAX/2;
rans = min(rans,test(-1,-1));
rans = min(rans,test(A,-1));
rans = min(rans,test(B,-1));
cout << rans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4204 KB |
Output is correct |
2 |
Correct |
4 ms |
4332 KB |
Output is correct |
3 |
Correct |
3 ms |
4204 KB |
Output is correct |
4 |
Correct |
3 ms |
4204 KB |
Output is correct |
5 |
Correct |
3 ms |
4204 KB |
Output is correct |
6 |
Correct |
4 ms |
4204 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
4 ms |
4332 KB |
Output is correct |
9 |
Correct |
3 ms |
4204 KB |
Output is correct |
10 |
Correct |
3 ms |
4204 KB |
Output is correct |
11 |
Correct |
52 ms |
4204 KB |
Output is correct |
12 |
Correct |
3 ms |
4204 KB |
Output is correct |
13 |
Correct |
4 ms |
4332 KB |
Output is correct |
14 |
Correct |
5 ms |
4332 KB |
Output is correct |
15 |
Correct |
37 ms |
4460 KB |
Output is correct |
16 |
Correct |
3 ms |
4204 KB |
Output is correct |
17 |
Correct |
4 ms |
4332 KB |
Output is correct |
18 |
Correct |
36 ms |
4332 KB |
Output is correct |
19 |
Correct |
4 ms |
4332 KB |
Output is correct |
20 |
Correct |
3 ms |
4332 KB |
Output is correct |
21 |
Correct |
3 ms |
4332 KB |
Output is correct |
22 |
Correct |
4 ms |
4204 KB |
Output is correct |
23 |
Correct |
4 ms |
4332 KB |
Output is correct |
24 |
Correct |
3 ms |
4204 KB |
Output is correct |
25 |
Correct |
3 ms |
4204 KB |
Output is correct |
26 |
Correct |
38 ms |
4204 KB |
Output is correct |
27 |
Correct |
3 ms |
4204 KB |
Output is correct |
28 |
Correct |
4 ms |
4332 KB |
Output is correct |
29 |
Correct |
4 ms |
4332 KB |
Output is correct |
30 |
Correct |
4 ms |
4332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4204 KB |
Output is correct |
2 |
Correct |
4 ms |
4332 KB |
Output is correct |
3 |
Correct |
3 ms |
4204 KB |
Output is correct |
4 |
Correct |
3 ms |
4204 KB |
Output is correct |
5 |
Correct |
3 ms |
4204 KB |
Output is correct |
6 |
Correct |
4 ms |
4204 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
4 ms |
4332 KB |
Output is correct |
9 |
Correct |
3 ms |
4204 KB |
Output is correct |
10 |
Correct |
3 ms |
4204 KB |
Output is correct |
11 |
Correct |
52 ms |
4204 KB |
Output is correct |
12 |
Correct |
3 ms |
4204 KB |
Output is correct |
13 |
Correct |
4 ms |
4332 KB |
Output is correct |
14 |
Correct |
5 ms |
4332 KB |
Output is correct |
15 |
Correct |
37 ms |
4460 KB |
Output is correct |
16 |
Correct |
3 ms |
4204 KB |
Output is correct |
17 |
Correct |
4 ms |
4332 KB |
Output is correct |
18 |
Correct |
36 ms |
4332 KB |
Output is correct |
19 |
Correct |
4 ms |
4332 KB |
Output is correct |
20 |
Correct |
3 ms |
4332 KB |
Output is correct |
21 |
Correct |
3 ms |
4332 KB |
Output is correct |
22 |
Correct |
4 ms |
4204 KB |
Output is correct |
23 |
Correct |
4 ms |
4332 KB |
Output is correct |
24 |
Correct |
3 ms |
4204 KB |
Output is correct |
25 |
Correct |
3 ms |
4204 KB |
Output is correct |
26 |
Correct |
38 ms |
4204 KB |
Output is correct |
27 |
Correct |
3 ms |
4204 KB |
Output is correct |
28 |
Correct |
4 ms |
4332 KB |
Output is correct |
29 |
Correct |
4 ms |
4332 KB |
Output is correct |
30 |
Correct |
4 ms |
4332 KB |
Output is correct |
31 |
Correct |
5 ms |
4332 KB |
Output is correct |
32 |
Correct |
5 ms |
4332 KB |
Output is correct |
33 |
Correct |
4 ms |
4332 KB |
Output is correct |
34 |
Correct |
37 ms |
4332 KB |
Output is correct |
35 |
Correct |
3 ms |
4204 KB |
Output is correct |
36 |
Correct |
6 ms |
4588 KB |
Output is correct |
37 |
Correct |
40 ms |
4716 KB |
Output is correct |
38 |
Correct |
37 ms |
4332 KB |
Output is correct |
39 |
Correct |
35 ms |
9856 KB |
Output is correct |
40 |
Correct |
11 ms |
5632 KB |
Output is correct |
41 |
Correct |
4 ms |
4332 KB |
Output is correct |
42 |
Correct |
11 ms |
5632 KB |
Output is correct |
43 |
Correct |
5 ms |
4460 KB |
Output is correct |
44 |
Correct |
4 ms |
4332 KB |
Output is correct |
45 |
Correct |
3 ms |
4332 KB |
Output is correct |
46 |
Correct |
39 ms |
10092 KB |
Output is correct |
47 |
Correct |
9 ms |
5100 KB |
Output is correct |
48 |
Correct |
76 ms |
9804 KB |
Output is correct |
49 |
Correct |
34 ms |
9836 KB |
Output is correct |
50 |
Correct |
38 ms |
4332 KB |
Output is correct |
51 |
Correct |
5 ms |
4332 KB |
Output is correct |
52 |
Correct |
4 ms |
4332 KB |
Output is correct |
53 |
Correct |
21 ms |
7148 KB |
Output is correct |
54 |
Correct |
37 ms |
9964 KB |
Output is correct |
55 |
Correct |
4 ms |
4332 KB |
Output is correct |
56 |
Correct |
4 ms |
4332 KB |
Output is correct |
57 |
Correct |
4 ms |
4332 KB |
Output is correct |
58 |
Correct |
80 ms |
10348 KB |
Output is correct |
59 |
Correct |
3 ms |
4204 KB |
Output is correct |
60 |
Correct |
5 ms |
4332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
9736 KB |
Output is correct |
2 |
Correct |
12 ms |
4972 KB |
Output is correct |
3 |
Correct |
2317 ms |
163092 KB |
Output is correct |
4 |
Correct |
1273 ms |
78832 KB |
Output is correct |
5 |
Correct |
486 ms |
18384 KB |
Output is correct |
6 |
Correct |
391 ms |
14788 KB |
Output is correct |
7 |
Correct |
522 ms |
20100 KB |
Output is correct |
8 |
Correct |
192 ms |
9804 KB |
Output is correct |
9 |
Correct |
355 ms |
13980 KB |
Output is correct |
10 |
Correct |
282 ms |
11112 KB |
Output is correct |
11 |
Correct |
2435 ms |
172564 KB |
Output is correct |
12 |
Correct |
289 ms |
11780 KB |
Output is correct |
13 |
Correct |
836 ms |
49176 KB |
Output is correct |
14 |
Correct |
526 ms |
21168 KB |
Output is correct |
15 |
Correct |
2438 ms |
169528 KB |
Output is correct |
16 |
Correct |
122 ms |
8740 KB |
Output is correct |
17 |
Correct |
1923 ms |
124704 KB |
Output is correct |
18 |
Correct |
11 ms |
5100 KB |
Output is correct |
19 |
Correct |
2783 ms |
183308 KB |
Output is correct |
20 |
Correct |
497 ms |
20020 KB |
Output is correct |
21 |
Correct |
465 ms |
17348 KB |
Output is correct |
22 |
Correct |
241 ms |
10904 KB |
Output is correct |
23 |
Correct |
37 ms |
7532 KB |
Output is correct |
24 |
Correct |
1668 ms |
134020 KB |
Output is correct |
25 |
Correct |
381 ms |
14356 KB |
Output is correct |
26 |
Correct |
196 ms |
9836 KB |
Output is correct |
27 |
Correct |
324 ms |
12528 KB |
Output is correct |
28 |
Correct |
22 ms |
6252 KB |
Output is correct |
29 |
Correct |
501 ms |
19736 KB |
Output is correct |
30 |
Correct |
834 ms |
39368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4204 KB |
Output is correct |
2 |
Correct |
4 ms |
4332 KB |
Output is correct |
3 |
Correct |
3 ms |
4204 KB |
Output is correct |
4 |
Correct |
3 ms |
4204 KB |
Output is correct |
5 |
Correct |
3 ms |
4204 KB |
Output is correct |
6 |
Correct |
4 ms |
4204 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
4 ms |
4332 KB |
Output is correct |
9 |
Correct |
3 ms |
4204 KB |
Output is correct |
10 |
Correct |
3 ms |
4204 KB |
Output is correct |
11 |
Correct |
52 ms |
4204 KB |
Output is correct |
12 |
Correct |
3 ms |
4204 KB |
Output is correct |
13 |
Correct |
4 ms |
4332 KB |
Output is correct |
14 |
Correct |
5 ms |
4332 KB |
Output is correct |
15 |
Correct |
37 ms |
4460 KB |
Output is correct |
16 |
Correct |
3 ms |
4204 KB |
Output is correct |
17 |
Correct |
4 ms |
4332 KB |
Output is correct |
18 |
Correct |
36 ms |
4332 KB |
Output is correct |
19 |
Correct |
4 ms |
4332 KB |
Output is correct |
20 |
Correct |
3 ms |
4332 KB |
Output is correct |
21 |
Correct |
3 ms |
4332 KB |
Output is correct |
22 |
Correct |
4 ms |
4204 KB |
Output is correct |
23 |
Correct |
4 ms |
4332 KB |
Output is correct |
24 |
Correct |
3 ms |
4204 KB |
Output is correct |
25 |
Correct |
3 ms |
4204 KB |
Output is correct |
26 |
Correct |
38 ms |
4204 KB |
Output is correct |
27 |
Correct |
3 ms |
4204 KB |
Output is correct |
28 |
Correct |
4 ms |
4332 KB |
Output is correct |
29 |
Correct |
4 ms |
4332 KB |
Output is correct |
30 |
Correct |
4 ms |
4332 KB |
Output is correct |
31 |
Correct |
5 ms |
4332 KB |
Output is correct |
32 |
Correct |
5 ms |
4332 KB |
Output is correct |
33 |
Correct |
4 ms |
4332 KB |
Output is correct |
34 |
Correct |
37 ms |
4332 KB |
Output is correct |
35 |
Correct |
3 ms |
4204 KB |
Output is correct |
36 |
Correct |
6 ms |
4588 KB |
Output is correct |
37 |
Correct |
40 ms |
4716 KB |
Output is correct |
38 |
Correct |
37 ms |
4332 KB |
Output is correct |
39 |
Correct |
35 ms |
9856 KB |
Output is correct |
40 |
Correct |
11 ms |
5632 KB |
Output is correct |
41 |
Correct |
4 ms |
4332 KB |
Output is correct |
42 |
Correct |
11 ms |
5632 KB |
Output is correct |
43 |
Correct |
5 ms |
4460 KB |
Output is correct |
44 |
Correct |
4 ms |
4332 KB |
Output is correct |
45 |
Correct |
3 ms |
4332 KB |
Output is correct |
46 |
Correct |
39 ms |
10092 KB |
Output is correct |
47 |
Correct |
9 ms |
5100 KB |
Output is correct |
48 |
Correct |
76 ms |
9804 KB |
Output is correct |
49 |
Correct |
34 ms |
9836 KB |
Output is correct |
50 |
Correct |
38 ms |
4332 KB |
Output is correct |
51 |
Correct |
5 ms |
4332 KB |
Output is correct |
52 |
Correct |
4 ms |
4332 KB |
Output is correct |
53 |
Correct |
21 ms |
7148 KB |
Output is correct |
54 |
Correct |
37 ms |
9964 KB |
Output is correct |
55 |
Correct |
4 ms |
4332 KB |
Output is correct |
56 |
Correct |
4 ms |
4332 KB |
Output is correct |
57 |
Correct |
4 ms |
4332 KB |
Output is correct |
58 |
Correct |
80 ms |
10348 KB |
Output is correct |
59 |
Correct |
3 ms |
4204 KB |
Output is correct |
60 |
Correct |
5 ms |
4332 KB |
Output is correct |
61 |
Correct |
202 ms |
9736 KB |
Output is correct |
62 |
Correct |
12 ms |
4972 KB |
Output is correct |
63 |
Correct |
2317 ms |
163092 KB |
Output is correct |
64 |
Correct |
1273 ms |
78832 KB |
Output is correct |
65 |
Correct |
486 ms |
18384 KB |
Output is correct |
66 |
Correct |
391 ms |
14788 KB |
Output is correct |
67 |
Correct |
522 ms |
20100 KB |
Output is correct |
68 |
Correct |
192 ms |
9804 KB |
Output is correct |
69 |
Correct |
355 ms |
13980 KB |
Output is correct |
70 |
Correct |
282 ms |
11112 KB |
Output is correct |
71 |
Correct |
2435 ms |
172564 KB |
Output is correct |
72 |
Correct |
289 ms |
11780 KB |
Output is correct |
73 |
Correct |
836 ms |
49176 KB |
Output is correct |
74 |
Correct |
526 ms |
21168 KB |
Output is correct |
75 |
Correct |
2438 ms |
169528 KB |
Output is correct |
76 |
Correct |
122 ms |
8740 KB |
Output is correct |
77 |
Correct |
1923 ms |
124704 KB |
Output is correct |
78 |
Correct |
11 ms |
5100 KB |
Output is correct |
79 |
Correct |
2783 ms |
183308 KB |
Output is correct |
80 |
Correct |
497 ms |
20020 KB |
Output is correct |
81 |
Correct |
465 ms |
17348 KB |
Output is correct |
82 |
Correct |
241 ms |
10904 KB |
Output is correct |
83 |
Correct |
37 ms |
7532 KB |
Output is correct |
84 |
Correct |
1668 ms |
134020 KB |
Output is correct |
85 |
Correct |
381 ms |
14356 KB |
Output is correct |
86 |
Correct |
196 ms |
9836 KB |
Output is correct |
87 |
Correct |
324 ms |
12528 KB |
Output is correct |
88 |
Correct |
22 ms |
6252 KB |
Output is correct |
89 |
Correct |
501 ms |
19736 KB |
Output is correct |
90 |
Correct |
834 ms |
39368 KB |
Output is correct |
91 |
Correct |
348 ms |
13292 KB |
Output is correct |
92 |
Correct |
2527 ms |
186424 KB |
Output is correct |
93 |
Correct |
522 ms |
20460 KB |
Output is correct |
94 |
Correct |
1867 ms |
116072 KB |
Output is correct |
95 |
Correct |
11 ms |
4972 KB |
Output is correct |
96 |
Correct |
13 ms |
5228 KB |
Output is correct |
97 |
Correct |
624 ms |
28364 KB |
Output is correct |
98 |
Correct |
336 ms |
13224 KB |
Output is correct |
99 |
Correct |
382 ms |
14572 KB |
Output is correct |
100 |
Correct |
2918 ms |
204216 KB |
Output is correct |
101 |
Correct |
1402 ms |
79328 KB |
Output is correct |
102 |
Correct |
1344 ms |
78344 KB |
Output is correct |
103 |
Correct |
1775 ms |
138892 KB |
Output is correct |
104 |
Correct |
2476 ms |
161756 KB |
Output is correct |
105 |
Correct |
203 ms |
9964 KB |
Output is correct |
106 |
Correct |
1943 ms |
143132 KB |
Output is correct |
107 |
Correct |
906 ms |
42548 KB |
Output is correct |
108 |
Correct |
481 ms |
18780 KB |
Output is correct |
109 |
Correct |
30 ms |
6636 KB |
Output is correct |
110 |
Correct |
155 ms |
9348 KB |
Output is correct |
111 |
Correct |
490 ms |
19304 KB |
Output is correct |
112 |
Correct |
2890 ms |
203352 KB |
Output is correct |
113 |
Correct |
3052 ms |
203972 KB |
Output is correct |
114 |
Correct |
506 ms |
18368 KB |
Output is correct |
115 |
Correct |
1139 ms |
70964 KB |
Output is correct |
116 |
Correct |
2627 ms |
182376 KB |
Output is correct |
117 |
Correct |
1840 ms |
136032 KB |
Output is correct |
118 |
Correct |
1756 ms |
123600 KB |
Output is correct |
119 |
Correct |
2703 ms |
165592 KB |
Output is correct |
120 |
Correct |
1899 ms |
102168 KB |
Output is correct |