#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>
using namespace std;
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long
const int MXN = 1e5 + 5, INF = 1e9 + 5;
int N, M, K;
int par[MXN];
vector<pair<int, int>> g[MXN];
vector<array<int, 3>> edges;
vector<int> dijkstra(const vector<int> &src) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
vector<int> dist(N, INF);
for (int i = 0; i < N; i++)
par[i] = i;
for (const auto &x : src) {
dist[x] = 0;
pq.emplace(0, x);
}
while (!pq.empty()) {
auto cur = pq.top();
pq.pop();
int u = cur.second, cur_dist = cur.first;
if (cur_dist != dist[u]) continue;
for (const auto &[v, wt] : g[u]) {
int new_dist = cur_dist + wt;
if (new_dist < dist[v]) {
dist[v] = new_dist;
pq.emplace(new_dist, v);
par[v] = par[u];
}
}
}
return dist;
}
array<int, 3> best_pair(const vector<int> &dist) {
pair<int, int> points = {-1, -1};
int points_dist = INF;
for (const auto &[u, v, w] : edges) {
if (par[u] == par[v]) continue;
int cur_dist = dist[u] + dist[v] + w;
if (cur_dist < points_dist) {
points.first = par[u];
points.second = par[v];
points_dist = cur_dist;
}
}
return array<int, 3>{points.first, points.second, points_dist};
}
void solve() {
cin >> N >> M >> K;
for (int i = 0; i < M; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
edges.push_back({u, v, w});
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
vector<int> special(K);
for (auto &x : special) {
cin >> x;
x--;
}
vector<int> dist_special = dijkstra(special);
array<int, 3> points1 = best_pair(dist_special);
vector<int> special_new;
for (const auto &x : special) {
if (x == points1[0] || x == points1[1]) continue;
special_new.push_back(x);
}
vector<int> dist_special_new = dijkstra(special_new);
array<int, 3> points2 = best_pair(dist_special_new);
int ans = points1[2] + points2[2];
vector<int> dist1 = dijkstra(vector<int>{points1[0]});
vector<int> dist2 = dijkstra(vector<int>{points1[1]});
set<pair<int, int>> dist2_set;
for (const auto &i : special) {
if (i == points1[0] || i == points1[1]) continue;
dist2_set.insert({dist2[i], i});
}
for (const auto &i : special) {
if (i == points1[0] || i == points1[1]) continue;
dist2_set.erase({dist2[i], i});
pair<int, int> dist2_best = *dist2_set.begin();
dist2_set.insert({dist2[i], i});
ans = min(ans, dist1[i] + dist2_best.first);
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int TC = 1;
// cin >> TC;
while (TC--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
2 ms |
2668 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
3 ms |
2668 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
3 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2668 KB |
Output is correct |
17 |
Correct |
2 ms |
2796 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
2 ms |
2796 KB |
Output is correct |
20 |
Correct |
2 ms |
2668 KB |
Output is correct |
21 |
Correct |
2 ms |
2796 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
2 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2668 KB |
Output is correct |
25 |
Correct |
2 ms |
2668 KB |
Output is correct |
26 |
Correct |
2 ms |
2668 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
2 ms |
2796 KB |
Output is correct |
29 |
Correct |
2 ms |
2796 KB |
Output is correct |
30 |
Correct |
2 ms |
2796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
2 ms |
2668 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
3 ms |
2668 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
3 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2668 KB |
Output is correct |
17 |
Correct |
2 ms |
2796 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
2 ms |
2796 KB |
Output is correct |
20 |
Correct |
2 ms |
2668 KB |
Output is correct |
21 |
Correct |
2 ms |
2796 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
2 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2668 KB |
Output is correct |
25 |
Correct |
2 ms |
2668 KB |
Output is correct |
26 |
Correct |
2 ms |
2668 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
2 ms |
2796 KB |
Output is correct |
29 |
Correct |
2 ms |
2796 KB |
Output is correct |
30 |
Correct |
2 ms |
2796 KB |
Output is correct |
31 |
Correct |
2 ms |
2668 KB |
Output is correct |
32 |
Correct |
3 ms |
2944 KB |
Output is correct |
33 |
Correct |
3 ms |
2796 KB |
Output is correct |
34 |
Correct |
2 ms |
2668 KB |
Output is correct |
35 |
Correct |
2 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
2924 KB |
Output is correct |
37 |
Correct |
5 ms |
3052 KB |
Output is correct |
38 |
Correct |
3 ms |
2796 KB |
Output is correct |
39 |
Correct |
32 ms |
7076 KB |
Output is correct |
40 |
Correct |
11 ms |
3820 KB |
Output is correct |
41 |
Correct |
3 ms |
2796 KB |
Output is correct |
42 |
Correct |
9 ms |
3820 KB |
Output is correct |
43 |
Correct |
4 ms |
2924 KB |
Output is correct |
44 |
Correct |
3 ms |
2796 KB |
Output is correct |
45 |
Correct |
2 ms |
2668 KB |
Output is correct |
46 |
Correct |
35 ms |
7456 KB |
Output is correct |
47 |
Correct |
7 ms |
3308 KB |
Output is correct |
48 |
Correct |
28 ms |
7076 KB |
Output is correct |
49 |
Correct |
30 ms |
7204 KB |
Output is correct |
50 |
Correct |
2 ms |
2796 KB |
Output is correct |
51 |
Correct |
3 ms |
2796 KB |
Output is correct |
52 |
Correct |
2 ms |
2796 KB |
Output is correct |
53 |
Correct |
18 ms |
5028 KB |
Output is correct |
54 |
Correct |
31 ms |
7076 KB |
Output is correct |
55 |
Correct |
2 ms |
2796 KB |
Output is correct |
56 |
Correct |
2 ms |
2668 KB |
Output is correct |
57 |
Correct |
3 ms |
2796 KB |
Output is correct |
58 |
Correct |
42 ms |
7840 KB |
Output is correct |
59 |
Correct |
2 ms |
2668 KB |
Output is correct |
60 |
Correct |
3 ms |
2796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
9232 KB |
Output is correct |
2 |
Correct |
8 ms |
5356 KB |
Output is correct |
3 |
Correct |
2049 ms |
139696 KB |
Output is correct |
4 |
Correct |
1012 ms |
61056 KB |
Output is correct |
5 |
Correct |
196 ms |
14876 KB |
Output is correct |
6 |
Correct |
153 ms |
12856 KB |
Output is correct |
7 |
Correct |
235 ms |
16304 KB |
Output is correct |
8 |
Correct |
86 ms |
9376 KB |
Output is correct |
9 |
Correct |
145 ms |
12196 KB |
Output is correct |
10 |
Correct |
105 ms |
10408 KB |
Output is correct |
11 |
Correct |
2128 ms |
142220 KB |
Output is correct |
12 |
Correct |
110 ms |
10784 KB |
Output is correct |
13 |
Correct |
774 ms |
40576 KB |
Output is correct |
14 |
Correct |
349 ms |
17216 KB |
Output is correct |
15 |
Correct |
2013 ms |
139668 KB |
Output is correct |
16 |
Correct |
54 ms |
8868 KB |
Output is correct |
17 |
Correct |
1369 ms |
94720 KB |
Output is correct |
18 |
Correct |
8 ms |
5484 KB |
Output is correct |
19 |
Correct |
2085 ms |
144512 KB |
Output is correct |
20 |
Correct |
209 ms |
15900 KB |
Output is correct |
21 |
Correct |
184 ms |
14236 KB |
Output is correct |
22 |
Correct |
102 ms |
10016 KB |
Output is correct |
23 |
Correct |
28 ms |
7716 KB |
Output is correct |
24 |
Correct |
1549 ms |
106476 KB |
Output is correct |
25 |
Correct |
157 ms |
12192 KB |
Output is correct |
26 |
Correct |
85 ms |
9384 KB |
Output is correct |
27 |
Correct |
115 ms |
11040 KB |
Output is correct |
28 |
Correct |
17 ms |
6568 KB |
Output is correct |
29 |
Correct |
203 ms |
15900 KB |
Output is correct |
30 |
Correct |
527 ms |
29840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
2 ms |
2668 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
3 ms |
2668 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
3 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2668 KB |
Output is correct |
17 |
Correct |
2 ms |
2796 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
2 ms |
2796 KB |
Output is correct |
20 |
Correct |
2 ms |
2668 KB |
Output is correct |
21 |
Correct |
2 ms |
2796 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
2 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2668 KB |
Output is correct |
25 |
Correct |
2 ms |
2668 KB |
Output is correct |
26 |
Correct |
2 ms |
2668 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
2 ms |
2796 KB |
Output is correct |
29 |
Correct |
2 ms |
2796 KB |
Output is correct |
30 |
Correct |
2 ms |
2796 KB |
Output is correct |
31 |
Correct |
2 ms |
2668 KB |
Output is correct |
32 |
Correct |
3 ms |
2944 KB |
Output is correct |
33 |
Correct |
3 ms |
2796 KB |
Output is correct |
34 |
Correct |
2 ms |
2668 KB |
Output is correct |
35 |
Correct |
2 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
2924 KB |
Output is correct |
37 |
Correct |
5 ms |
3052 KB |
Output is correct |
38 |
Correct |
3 ms |
2796 KB |
Output is correct |
39 |
Correct |
32 ms |
7076 KB |
Output is correct |
40 |
Correct |
11 ms |
3820 KB |
Output is correct |
41 |
Correct |
3 ms |
2796 KB |
Output is correct |
42 |
Correct |
9 ms |
3820 KB |
Output is correct |
43 |
Correct |
4 ms |
2924 KB |
Output is correct |
44 |
Correct |
3 ms |
2796 KB |
Output is correct |
45 |
Correct |
2 ms |
2668 KB |
Output is correct |
46 |
Correct |
35 ms |
7456 KB |
Output is correct |
47 |
Correct |
7 ms |
3308 KB |
Output is correct |
48 |
Correct |
28 ms |
7076 KB |
Output is correct |
49 |
Correct |
30 ms |
7204 KB |
Output is correct |
50 |
Correct |
2 ms |
2796 KB |
Output is correct |
51 |
Correct |
3 ms |
2796 KB |
Output is correct |
52 |
Correct |
2 ms |
2796 KB |
Output is correct |
53 |
Correct |
18 ms |
5028 KB |
Output is correct |
54 |
Correct |
31 ms |
7076 KB |
Output is correct |
55 |
Correct |
2 ms |
2796 KB |
Output is correct |
56 |
Correct |
2 ms |
2668 KB |
Output is correct |
57 |
Correct |
3 ms |
2796 KB |
Output is correct |
58 |
Correct |
42 ms |
7840 KB |
Output is correct |
59 |
Correct |
2 ms |
2668 KB |
Output is correct |
60 |
Correct |
3 ms |
2796 KB |
Output is correct |
61 |
Correct |
70 ms |
9232 KB |
Output is correct |
62 |
Correct |
8 ms |
5356 KB |
Output is correct |
63 |
Correct |
2049 ms |
139696 KB |
Output is correct |
64 |
Correct |
1012 ms |
61056 KB |
Output is correct |
65 |
Correct |
196 ms |
14876 KB |
Output is correct |
66 |
Correct |
153 ms |
12856 KB |
Output is correct |
67 |
Correct |
235 ms |
16304 KB |
Output is correct |
68 |
Correct |
86 ms |
9376 KB |
Output is correct |
69 |
Correct |
145 ms |
12196 KB |
Output is correct |
70 |
Correct |
105 ms |
10408 KB |
Output is correct |
71 |
Correct |
2128 ms |
142220 KB |
Output is correct |
72 |
Correct |
110 ms |
10784 KB |
Output is correct |
73 |
Correct |
774 ms |
40576 KB |
Output is correct |
74 |
Correct |
349 ms |
17216 KB |
Output is correct |
75 |
Correct |
2013 ms |
139668 KB |
Output is correct |
76 |
Correct |
54 ms |
8868 KB |
Output is correct |
77 |
Correct |
1369 ms |
94720 KB |
Output is correct |
78 |
Correct |
8 ms |
5484 KB |
Output is correct |
79 |
Correct |
2085 ms |
144512 KB |
Output is correct |
80 |
Correct |
209 ms |
15900 KB |
Output is correct |
81 |
Correct |
184 ms |
14236 KB |
Output is correct |
82 |
Correct |
102 ms |
10016 KB |
Output is correct |
83 |
Correct |
28 ms |
7716 KB |
Output is correct |
84 |
Correct |
1549 ms |
106476 KB |
Output is correct |
85 |
Correct |
157 ms |
12192 KB |
Output is correct |
86 |
Correct |
85 ms |
9384 KB |
Output is correct |
87 |
Correct |
115 ms |
11040 KB |
Output is correct |
88 |
Correct |
17 ms |
6568 KB |
Output is correct |
89 |
Correct |
203 ms |
15900 KB |
Output is correct |
90 |
Correct |
527 ms |
29840 KB |
Output is correct |
91 |
Correct |
191 ms |
12064 KB |
Output is correct |
92 |
Correct |
2145 ms |
153544 KB |
Output is correct |
93 |
Correct |
301 ms |
17052 KB |
Output is correct |
94 |
Correct |
1300 ms |
88164 KB |
Output is correct |
95 |
Correct |
8 ms |
5484 KB |
Output is correct |
96 |
Correct |
9 ms |
5612 KB |
Output is correct |
97 |
Correct |
413 ms |
22160 KB |
Output is correct |
98 |
Correct |
195 ms |
12064 KB |
Output is correct |
99 |
Correct |
154 ms |
12448 KB |
Output is correct |
100 |
Correct |
2230 ms |
163028 KB |
Output is correct |
101 |
Correct |
967 ms |
59648 KB |
Output is correct |
102 |
Correct |
944 ms |
58880 KB |
Output is correct |
103 |
Correct |
1654 ms |
109896 KB |
Output is correct |
104 |
Correct |
1954 ms |
139560 KB |
Output is correct |
105 |
Correct |
124 ms |
9632 KB |
Output is correct |
106 |
Correct |
1640 ms |
110228 KB |
Output is correct |
107 |
Correct |
615 ms |
32408 KB |
Output is correct |
108 |
Correct |
319 ms |
15644 KB |
Output is correct |
109 |
Correct |
23 ms |
7080 KB |
Output is correct |
110 |
Correct |
88 ms |
9136 KB |
Output is correct |
111 |
Correct |
314 ms |
16176 KB |
Output is correct |
112 |
Correct |
2368 ms |
163048 KB |
Output is correct |
113 |
Correct |
2382 ms |
164300 KB |
Output is correct |
114 |
Correct |
298 ms |
15644 KB |
Output is correct |
115 |
Correct |
969 ms |
57088 KB |
Output is correct |
116 |
Correct |
2193 ms |
150436 KB |
Output is correct |
117 |
Correct |
1568 ms |
107496 KB |
Output is correct |
118 |
Correct |
1439 ms |
96064 KB |
Output is correct |
119 |
Correct |
1950 ms |
139724 KB |
Output is correct |
120 |
Correct |
1215 ms |
78552 KB |
Output is correct |