#include <bits/stdc++.h>
using namespace std;
int n, m, k;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int a, int b) {
return a + (unsigned long long)rng() % (b - a + 1);
}
const int oo = 1e9+10;
int calc(vector<int> &a, vector<int> &b, vector<vector<pair<int, int>>> &edges) {
priority_queue<pair<int,int>> q;
for(int x : a) {
q.push({0, x});
}
vector<bool> fix(n), seen(n);
for(int x : b) {
fix[x] = 1;
}
while(!q.empty()) {
pair<int,int> p = q.top(); q.pop();
if(fix[p.second]) return -p.first;
if(seen[p.second]) continue;
seen[p.second] = 1;
for(const pair<int,int> &to : edges[p.second]) {
q.push({p.first - to.second, to.first});
}
}
return oo;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> k;
vector<vector<pair<int, int>>> edges(n);
for(int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w; --u, --v;
edges[u].push_back({v, w});
edges[v].push_back({u, w});
}
vector<int> special(k);
for(int &i : special) cin >> i, --i;
int ans = oo;
for(int it = 0; it < 200; ++it) {
shuffle(special.begin(), special.end(), rng);
vector<int> start[2], end[2];
for(int i = 0; i < k / 2; ++i) {
start[rand(0, 1)].push_back(special[i]);
}
for(int i = k / 2; i < k; ++i) {
end[rand(0, 1)].push_back(special[i]);
}
ans = min(ans, calc(start[0], end[0], edges) + calc(start[1], end[1], edges));
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
9 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
9 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
7 ms |
364 KB |
Output is correct |
14 |
Correct |
10 ms |
364 KB |
Output is correct |
15 |
Correct |
8 ms |
364 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
6 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
236 KB |
Output is correct |
19 |
Correct |
8 ms |
364 KB |
Output is correct |
20 |
Correct |
3 ms |
364 KB |
Output is correct |
21 |
Correct |
7 ms |
364 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
3 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
8 ms |
364 KB |
Output is correct |
29 |
Correct |
6 ms |
364 KB |
Output is correct |
30 |
Correct |
8 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
9 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
9 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
7 ms |
364 KB |
Output is correct |
14 |
Correct |
10 ms |
364 KB |
Output is correct |
15 |
Correct |
8 ms |
364 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
6 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
236 KB |
Output is correct |
19 |
Correct |
8 ms |
364 KB |
Output is correct |
20 |
Correct |
3 ms |
364 KB |
Output is correct |
21 |
Correct |
7 ms |
364 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
3 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
8 ms |
364 KB |
Output is correct |
29 |
Correct |
6 ms |
364 KB |
Output is correct |
30 |
Correct |
8 ms |
364 KB |
Output is correct |
31 |
Correct |
8 ms |
436 KB |
Output is correct |
32 |
Correct |
12 ms |
364 KB |
Output is correct |
33 |
Correct |
23 ms |
364 KB |
Output is correct |
34 |
Correct |
4 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
22 ms |
492 KB |
Output is correct |
37 |
Correct |
43 ms |
748 KB |
Output is correct |
38 |
Correct |
12 ms |
364 KB |
Output is correct |
39 |
Correct |
325 ms |
5388 KB |
Output is correct |
40 |
Correct |
70 ms |
1260 KB |
Output is correct |
41 |
Correct |
15 ms |
364 KB |
Output is correct |
42 |
Correct |
86 ms |
1448 KB |
Output is correct |
43 |
Correct |
19 ms |
568 KB |
Output is correct |
44 |
Correct |
18 ms |
364 KB |
Output is correct |
45 |
Correct |
2 ms |
364 KB |
Output is correct |
46 |
Correct |
637 ms |
5020 KB |
Output is correct |
47 |
Correct |
52 ms |
876 KB |
Output is correct |
48 |
Correct |
179 ms |
4452 KB |
Output is correct |
49 |
Correct |
309 ms |
4536 KB |
Output is correct |
50 |
Correct |
15 ms |
364 KB |
Output is correct |
51 |
Correct |
12 ms |
364 KB |
Output is correct |
52 |
Correct |
12 ms |
364 KB |
Output is correct |
53 |
Correct |
109 ms |
2572 KB |
Output is correct |
54 |
Correct |
184 ms |
4548 KB |
Output is correct |
55 |
Correct |
13 ms |
364 KB |
Output is correct |
56 |
Correct |
4 ms |
364 KB |
Output is correct |
57 |
Correct |
9 ms |
364 KB |
Output is correct |
58 |
Correct |
315 ms |
4880 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
12 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
546 ms |
5692 KB |
Output is correct |
2 |
Correct |
7 ms |
3308 KB |
Output is correct |
3 |
Execution timed out |
6081 ms |
114672 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
9 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
9 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
7 ms |
364 KB |
Output is correct |
14 |
Correct |
10 ms |
364 KB |
Output is correct |
15 |
Correct |
8 ms |
364 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
6 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
236 KB |
Output is correct |
19 |
Correct |
8 ms |
364 KB |
Output is correct |
20 |
Correct |
3 ms |
364 KB |
Output is correct |
21 |
Correct |
7 ms |
364 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
3 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
8 ms |
364 KB |
Output is correct |
29 |
Correct |
6 ms |
364 KB |
Output is correct |
30 |
Correct |
8 ms |
364 KB |
Output is correct |
31 |
Correct |
8 ms |
436 KB |
Output is correct |
32 |
Correct |
12 ms |
364 KB |
Output is correct |
33 |
Correct |
23 ms |
364 KB |
Output is correct |
34 |
Correct |
4 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
22 ms |
492 KB |
Output is correct |
37 |
Correct |
43 ms |
748 KB |
Output is correct |
38 |
Correct |
12 ms |
364 KB |
Output is correct |
39 |
Correct |
325 ms |
5388 KB |
Output is correct |
40 |
Correct |
70 ms |
1260 KB |
Output is correct |
41 |
Correct |
15 ms |
364 KB |
Output is correct |
42 |
Correct |
86 ms |
1448 KB |
Output is correct |
43 |
Correct |
19 ms |
568 KB |
Output is correct |
44 |
Correct |
18 ms |
364 KB |
Output is correct |
45 |
Correct |
2 ms |
364 KB |
Output is correct |
46 |
Correct |
637 ms |
5020 KB |
Output is correct |
47 |
Correct |
52 ms |
876 KB |
Output is correct |
48 |
Correct |
179 ms |
4452 KB |
Output is correct |
49 |
Correct |
309 ms |
4536 KB |
Output is correct |
50 |
Correct |
15 ms |
364 KB |
Output is correct |
51 |
Correct |
12 ms |
364 KB |
Output is correct |
52 |
Correct |
12 ms |
364 KB |
Output is correct |
53 |
Correct |
109 ms |
2572 KB |
Output is correct |
54 |
Correct |
184 ms |
4548 KB |
Output is correct |
55 |
Correct |
13 ms |
364 KB |
Output is correct |
56 |
Correct |
4 ms |
364 KB |
Output is correct |
57 |
Correct |
9 ms |
364 KB |
Output is correct |
58 |
Correct |
315 ms |
4880 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
12 ms |
492 KB |
Output is correct |
61 |
Correct |
546 ms |
5692 KB |
Output is correct |
62 |
Correct |
7 ms |
3308 KB |
Output is correct |
63 |
Execution timed out |
6081 ms |
114672 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |