#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(a) (a).begin(), (a).end()
const int N=1e5+1;
int n, m, k, dd[N];
int dist[N], par[N], d1[N], d2[N];
vector<pair<int, int>> g[N];
pair<pair<int, int>, pair<int, int>> dijkstra(int root, int d[]){
memset(d, 0x3f, sizeof(int)*N);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
d[root]=0;
pq.emplace(d[root], root);
pair<pair<int, int>, pair<int, int>> ans={{1e18, -1}, {1e18, -1}};
while (pq.size()){
int u=pq.top().second, f=pq.top().first;
pq.pop();
if (d[u]!=f) continue;
if (dd[u]){
if (ans.first.first>d[u]){
ans.second=ans.first;
ans.first={d[u], u};
}else ans.second=min(ans.second, {d[u], u});
}
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (d[v]>d[u]+w){
d[v]=d[u]+w;
pq.emplace(d[v], v);
}
}
}
return ans;
}
pair<int, pair<int, int>> dijkstra(vector<int> root){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
memset(dist, 0x3f, sizeof dist);
for (int x:root){
dist[x]=0; par[x]=x;
pq.emplace(dist[x], x);
}
pair<int, pair<int, int>> best={-1, {-1, -1}};
while (pq.size()){
int u=pq.top().second, d=pq.top().first; pq.pop();
if (dist[u]!=d) continue;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (par[u]!=par[v]){
if (best.first==-1) best={dist[u]+dist[v]+w, {par[u], par[v]}};
else best=min(best, {dist[u]+dist[v]+w, {par[u], par[v]}});
}
if (dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
par[v]=par[u];
pq.emplace(dist[v], v);
}
}
}
return best;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
for (int i=1; i<=m; ++i){
int u, v, w; cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
vector<int> v(k);
for (int &i:v) cin >> i, dd[i]=1;
auto best1=dijkstra(v);
int t1=best1.second.first, t2=best1.second.second;
v.erase(find(all(v), t1));
v.erase(find(all(v), t2));
dd[t1]=dd[t2]=0;
auto g1=dijkstra(t1, d1);
auto g2=dijkstra(t2, d2);
auto best2=dijkstra(v);
int ans=best1.first+best2.first;
if (g1.first.second!=-1 && g2.first.second!=-1 && g1.first.second!=g2.first.second){
ans=min(ans, g1.first.first+g2.first.first);
}
if (g1.first.second!=-1 && g2.second.second!=-1 && g1.first.second!=g2.second.second){
ans=min(ans, g1.first.first+g2.second.first);
}
if (g1.second.second!=-1 && g2.first.second!=-1 && g1.second.second!=g2.first.second){
ans=min(ans, g1.second.first+g2.first.first);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5516 KB |
Output is correct |
4 |
Correct |
1 ms |
5468 KB |
Output is correct |
5 |
Correct |
1 ms |
5464 KB |
Output is correct |
6 |
Correct |
1 ms |
5468 KB |
Output is correct |
7 |
Correct |
1 ms |
5464 KB |
Output is correct |
8 |
Correct |
2 ms |
5468 KB |
Output is correct |
9 |
Correct |
1 ms |
5468 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
1 ms |
5468 KB |
Output is correct |
12 |
Correct |
1 ms |
5468 KB |
Output is correct |
13 |
Correct |
1 ms |
5520 KB |
Output is correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
1 ms |
5468 KB |
Output is correct |
17 |
Correct |
1 ms |
5720 KB |
Output is correct |
18 |
Correct |
1 ms |
5468 KB |
Output is correct |
19 |
Correct |
1 ms |
5468 KB |
Output is correct |
20 |
Correct |
1 ms |
5468 KB |
Output is correct |
21 |
Correct |
1 ms |
5468 KB |
Output is correct |
22 |
Correct |
2 ms |
5468 KB |
Output is correct |
23 |
Correct |
1 ms |
5468 KB |
Output is correct |
24 |
Correct |
1 ms |
5720 KB |
Output is correct |
25 |
Correct |
1 ms |
5464 KB |
Output is correct |
26 |
Correct |
1 ms |
5468 KB |
Output is correct |
27 |
Correct |
1 ms |
5468 KB |
Output is correct |
28 |
Correct |
1 ms |
5468 KB |
Output is correct |
29 |
Correct |
1 ms |
5520 KB |
Output is correct |
30 |
Correct |
1 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5516 KB |
Output is correct |
4 |
Correct |
1 ms |
5468 KB |
Output is correct |
5 |
Correct |
1 ms |
5464 KB |
Output is correct |
6 |
Correct |
1 ms |
5468 KB |
Output is correct |
7 |
Correct |
1 ms |
5464 KB |
Output is correct |
8 |
Correct |
2 ms |
5468 KB |
Output is correct |
9 |
Correct |
1 ms |
5468 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
1 ms |
5468 KB |
Output is correct |
12 |
Correct |
1 ms |
5468 KB |
Output is correct |
13 |
Correct |
1 ms |
5520 KB |
Output is correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
1 ms |
5468 KB |
Output is correct |
17 |
Correct |
1 ms |
5720 KB |
Output is correct |
18 |
Correct |
1 ms |
5468 KB |
Output is correct |
19 |
Correct |
1 ms |
5468 KB |
Output is correct |
20 |
Correct |
1 ms |
5468 KB |
Output is correct |
21 |
Correct |
1 ms |
5468 KB |
Output is correct |
22 |
Correct |
2 ms |
5468 KB |
Output is correct |
23 |
Correct |
1 ms |
5468 KB |
Output is correct |
24 |
Correct |
1 ms |
5720 KB |
Output is correct |
25 |
Correct |
1 ms |
5464 KB |
Output is correct |
26 |
Correct |
1 ms |
5468 KB |
Output is correct |
27 |
Correct |
1 ms |
5468 KB |
Output is correct |
28 |
Correct |
1 ms |
5468 KB |
Output is correct |
29 |
Correct |
1 ms |
5520 KB |
Output is correct |
30 |
Correct |
1 ms |
5468 KB |
Output is correct |
31 |
Correct |
1 ms |
5468 KB |
Output is correct |
32 |
Correct |
2 ms |
5468 KB |
Output is correct |
33 |
Correct |
2 ms |
5468 KB |
Output is correct |
34 |
Correct |
1 ms |
5468 KB |
Output is correct |
35 |
Correct |
1 ms |
5468 KB |
Output is correct |
36 |
Correct |
2 ms |
5724 KB |
Output is correct |
37 |
Correct |
3 ms |
5980 KB |
Output is correct |
38 |
Correct |
2 ms |
5464 KB |
Output is correct |
39 |
Correct |
18 ms |
10844 KB |
Output is correct |
40 |
Correct |
6 ms |
6740 KB |
Output is correct |
41 |
Correct |
2 ms |
5468 KB |
Output is correct |
42 |
Correct |
6 ms |
6736 KB |
Output is correct |
43 |
Correct |
3 ms |
5724 KB |
Output is correct |
44 |
Correct |
2 ms |
5464 KB |
Output is correct |
45 |
Correct |
1 ms |
5468 KB |
Output is correct |
46 |
Correct |
21 ms |
11088 KB |
Output is correct |
47 |
Correct |
4 ms |
6236 KB |
Output is correct |
48 |
Correct |
17 ms |
10804 KB |
Output is correct |
49 |
Correct |
19 ms |
10844 KB |
Output is correct |
50 |
Correct |
2 ms |
5468 KB |
Output is correct |
51 |
Correct |
2 ms |
5468 KB |
Output is correct |
52 |
Correct |
2 ms |
5468 KB |
Output is correct |
53 |
Correct |
11 ms |
8336 KB |
Output is correct |
54 |
Correct |
19 ms |
11044 KB |
Output is correct |
55 |
Correct |
2 ms |
5468 KB |
Output is correct |
56 |
Correct |
1 ms |
5468 KB |
Output is correct |
57 |
Correct |
2 ms |
5468 KB |
Output is correct |
58 |
Correct |
25 ms |
11168 KB |
Output is correct |
59 |
Correct |
2 ms |
5468 KB |
Output is correct |
60 |
Correct |
2 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
9968 KB |
Output is correct |
2 |
Correct |
4 ms |
5980 KB |
Output is correct |
3 |
Correct |
999 ms |
125344 KB |
Output is correct |
4 |
Correct |
443 ms |
65544 KB |
Output is correct |
5 |
Correct |
112 ms |
16424 KB |
Output is correct |
6 |
Correct |
77 ms |
13808 KB |
Output is correct |
7 |
Correct |
118 ms |
17808 KB |
Output is correct |
8 |
Correct |
37 ms |
10076 KB |
Output is correct |
9 |
Correct |
75 ms |
13348 KB |
Output is correct |
10 |
Correct |
63 ms |
11324 KB |
Output is correct |
11 |
Correct |
1057 ms |
133148 KB |
Output is correct |
12 |
Correct |
63 ms |
11600 KB |
Output is correct |
13 |
Correct |
289 ms |
40644 KB |
Output is correct |
14 |
Correct |
132 ms |
18788 KB |
Output is correct |
15 |
Correct |
1084 ms |
130772 KB |
Output is correct |
16 |
Correct |
28 ms |
9304 KB |
Output is correct |
17 |
Correct |
741 ms |
98352 KB |
Output is correct |
18 |
Correct |
4 ms |
6236 KB |
Output is correct |
19 |
Correct |
1096 ms |
141428 KB |
Output is correct |
20 |
Correct |
108 ms |
17784 KB |
Output is correct |
21 |
Correct |
112 ms |
15812 KB |
Output is correct |
22 |
Correct |
45 ms |
11044 KB |
Output is correct |
23 |
Correct |
13 ms |
8536 KB |
Output is correct |
24 |
Correct |
720 ms |
107628 KB |
Output is correct |
25 |
Correct |
77 ms |
13368 KB |
Output is correct |
26 |
Correct |
40 ms |
10076 KB |
Output is correct |
27 |
Correct |
60 ms |
12108 KB |
Output is correct |
28 |
Correct |
8 ms |
7256 KB |
Output is correct |
29 |
Correct |
129 ms |
17992 KB |
Output is correct |
30 |
Correct |
228 ms |
32700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5516 KB |
Output is correct |
4 |
Correct |
1 ms |
5468 KB |
Output is correct |
5 |
Correct |
1 ms |
5464 KB |
Output is correct |
6 |
Correct |
1 ms |
5468 KB |
Output is correct |
7 |
Correct |
1 ms |
5464 KB |
Output is correct |
8 |
Correct |
2 ms |
5468 KB |
Output is correct |
9 |
Correct |
1 ms |
5468 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
1 ms |
5468 KB |
Output is correct |
12 |
Correct |
1 ms |
5468 KB |
Output is correct |
13 |
Correct |
1 ms |
5520 KB |
Output is correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
1 ms |
5468 KB |
Output is correct |
17 |
Correct |
1 ms |
5720 KB |
Output is correct |
18 |
Correct |
1 ms |
5468 KB |
Output is correct |
19 |
Correct |
1 ms |
5468 KB |
Output is correct |
20 |
Correct |
1 ms |
5468 KB |
Output is correct |
21 |
Correct |
1 ms |
5468 KB |
Output is correct |
22 |
Correct |
2 ms |
5468 KB |
Output is correct |
23 |
Correct |
1 ms |
5468 KB |
Output is correct |
24 |
Correct |
1 ms |
5720 KB |
Output is correct |
25 |
Correct |
1 ms |
5464 KB |
Output is correct |
26 |
Correct |
1 ms |
5468 KB |
Output is correct |
27 |
Correct |
1 ms |
5468 KB |
Output is correct |
28 |
Correct |
1 ms |
5468 KB |
Output is correct |
29 |
Correct |
1 ms |
5520 KB |
Output is correct |
30 |
Correct |
1 ms |
5468 KB |
Output is correct |
31 |
Correct |
1 ms |
5468 KB |
Output is correct |
32 |
Correct |
2 ms |
5468 KB |
Output is correct |
33 |
Correct |
2 ms |
5468 KB |
Output is correct |
34 |
Correct |
1 ms |
5468 KB |
Output is correct |
35 |
Correct |
1 ms |
5468 KB |
Output is correct |
36 |
Correct |
2 ms |
5724 KB |
Output is correct |
37 |
Correct |
3 ms |
5980 KB |
Output is correct |
38 |
Correct |
2 ms |
5464 KB |
Output is correct |
39 |
Correct |
18 ms |
10844 KB |
Output is correct |
40 |
Correct |
6 ms |
6740 KB |
Output is correct |
41 |
Correct |
2 ms |
5468 KB |
Output is correct |
42 |
Correct |
6 ms |
6736 KB |
Output is correct |
43 |
Correct |
3 ms |
5724 KB |
Output is correct |
44 |
Correct |
2 ms |
5464 KB |
Output is correct |
45 |
Correct |
1 ms |
5468 KB |
Output is correct |
46 |
Correct |
21 ms |
11088 KB |
Output is correct |
47 |
Correct |
4 ms |
6236 KB |
Output is correct |
48 |
Correct |
17 ms |
10804 KB |
Output is correct |
49 |
Correct |
19 ms |
10844 KB |
Output is correct |
50 |
Correct |
2 ms |
5468 KB |
Output is correct |
51 |
Correct |
2 ms |
5468 KB |
Output is correct |
52 |
Correct |
2 ms |
5468 KB |
Output is correct |
53 |
Correct |
11 ms |
8336 KB |
Output is correct |
54 |
Correct |
19 ms |
11044 KB |
Output is correct |
55 |
Correct |
2 ms |
5468 KB |
Output is correct |
56 |
Correct |
1 ms |
5468 KB |
Output is correct |
57 |
Correct |
2 ms |
5468 KB |
Output is correct |
58 |
Correct |
25 ms |
11168 KB |
Output is correct |
59 |
Correct |
2 ms |
5468 KB |
Output is correct |
60 |
Correct |
2 ms |
5468 KB |
Output is correct |
61 |
Correct |
36 ms |
9968 KB |
Output is correct |
62 |
Correct |
4 ms |
5980 KB |
Output is correct |
63 |
Correct |
999 ms |
125344 KB |
Output is correct |
64 |
Correct |
443 ms |
65544 KB |
Output is correct |
65 |
Correct |
112 ms |
16424 KB |
Output is correct |
66 |
Correct |
77 ms |
13808 KB |
Output is correct |
67 |
Correct |
118 ms |
17808 KB |
Output is correct |
68 |
Correct |
37 ms |
10076 KB |
Output is correct |
69 |
Correct |
75 ms |
13348 KB |
Output is correct |
70 |
Correct |
63 ms |
11324 KB |
Output is correct |
71 |
Correct |
1057 ms |
133148 KB |
Output is correct |
72 |
Correct |
63 ms |
11600 KB |
Output is correct |
73 |
Correct |
289 ms |
40644 KB |
Output is correct |
74 |
Correct |
132 ms |
18788 KB |
Output is correct |
75 |
Correct |
1084 ms |
130772 KB |
Output is correct |
76 |
Correct |
28 ms |
9304 KB |
Output is correct |
77 |
Correct |
741 ms |
98352 KB |
Output is correct |
78 |
Correct |
4 ms |
6236 KB |
Output is correct |
79 |
Correct |
1096 ms |
141428 KB |
Output is correct |
80 |
Correct |
108 ms |
17784 KB |
Output is correct |
81 |
Correct |
112 ms |
15812 KB |
Output is correct |
82 |
Correct |
45 ms |
11044 KB |
Output is correct |
83 |
Correct |
13 ms |
8536 KB |
Output is correct |
84 |
Correct |
720 ms |
107628 KB |
Output is correct |
85 |
Correct |
77 ms |
13368 KB |
Output is correct |
86 |
Correct |
40 ms |
10076 KB |
Output is correct |
87 |
Correct |
60 ms |
12108 KB |
Output is correct |
88 |
Correct |
8 ms |
7256 KB |
Output is correct |
89 |
Correct |
129 ms |
17992 KB |
Output is correct |
90 |
Correct |
228 ms |
32700 KB |
Output is correct |
91 |
Correct |
99 ms |
14444 KB |
Output is correct |
92 |
Correct |
1363 ms |
193172 KB |
Output is correct |
93 |
Correct |
168 ms |
21100 KB |
Output is correct |
94 |
Correct |
860 ms |
113680 KB |
Output is correct |
95 |
Correct |
4 ms |
6236 KB |
Output is correct |
96 |
Correct |
4 ms |
6548 KB |
Output is correct |
97 |
Correct |
244 ms |
28948 KB |
Output is correct |
98 |
Correct |
92 ms |
14504 KB |
Output is correct |
99 |
Correct |
75 ms |
15632 KB |
Output is correct |
100 |
Correct |
1419 ms |
206760 KB |
Output is correct |
101 |
Correct |
594 ms |
78440 KB |
Output is correct |
102 |
Correct |
604 ms |
76912 KB |
Output is correct |
103 |
Correct |
936 ms |
142840 KB |
Output is correct |
104 |
Correct |
1159 ms |
164780 KB |
Output is correct |
105 |
Correct |
57 ms |
11644 KB |
Output is correct |
106 |
Correct |
955 ms |
143600 KB |
Output is correct |
107 |
Correct |
349 ms |
42132 KB |
Output is correct |
108 |
Correct |
146 ms |
19428 KB |
Output is correct |
109 |
Correct |
15 ms |
8028 KB |
Output is correct |
110 |
Correct |
45 ms |
10588 KB |
Output is correct |
111 |
Correct |
157 ms |
19920 KB |
Output is correct |
112 |
Correct |
1417 ms |
210180 KB |
Output is correct |
113 |
Correct |
1396 ms |
208852 KB |
Output is correct |
114 |
Correct |
167 ms |
19128 KB |
Output is correct |
115 |
Correct |
505 ms |
75556 KB |
Output is correct |
116 |
Correct |
1274 ms |
189360 KB |
Output is correct |
117 |
Correct |
900 ms |
139848 KB |
Output is correct |
118 |
Correct |
850 ms |
124536 KB |
Output is correct |
119 |
Correct |
1227 ms |
169220 KB |
Output is correct |
120 |
Correct |
775 ms |
100156 KB |
Output is correct |