#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
const int MAXN=1e5+5,INF=1e9;
vector<pii> adj[MAXN];
int head[MAXN],dist[MAXN],n;
bool special[MAXN],skip[MAXN];
piii MinimumPairSpecialDistance(){
priority_queue<pii,vector<pii>,greater<pii>> PQ;
for(int i=1;i<=n;i++)dist[i]=INF;
for(int i=1;i<=n;i++){
if(!special[i] || skip[i])continue;
dist[i]=0;
head[i]=i;
PQ.push({dist[i],i});
}
while(!PQ.empty()){
pii now=PQ.top();
PQ.pop();
if(dist[now.second]<now.first)continue;
for(auto nxt : adj[now.second]){
if(!skip[nxt.first] && dist[now.second]+nxt.second<dist[nxt.first]){
dist[nxt.first]=dist[now.second]+nxt.second;
head[nxt.first]=head[now.second];
PQ.push({dist[nxt.first],nxt.first});
}
}
}
int val=INF;
pii ret;
for(int i=1;i<=n;i++){
for(auto j : adj[i]){
if(head[i]==head[j.first] || skip[head[i]] || skip[head[j.first]])continue;
if(dist[i]+j.second+dist[j.first]<val){
val=dist[i]+j.second+dist[j.first];
ret={head[i],head[j.first]};
}
}
}
return {val,ret};
}
int kasusdua(int a,int b){
priority_queue<pii,vector<pii>,greater<pii>> PQ;
for(int i=1;i<=n;i++)dist[i]=INF;
dist[a]=0;
PQ.push({dist[a],a});
while(!PQ.empty()){
pii now=PQ.top();
PQ.pop();
if(dist[now.second]<now.first)continue;
for(auto nxt : adj[now.second]){
if(dist[now.second]+nxt.second<dist[nxt.first]){
dist[nxt.first]=dist[now.second]+nxt.second;
PQ.push({dist[nxt.first],nxt.first});
}
}
}
vector<pii> jaraka;
for(int i=1;i<=n;i++){
if(i==a || i==b || !special[i])continue;
jaraka.push_back({dist[i],i});
}
sort(jaraka.begin(),jaraka.end());
for(int i=1;i<=n;i++)dist[i]=INF;
dist[b]=0;
PQ.push({dist[b],b});
while(!PQ.empty()){
pii now=PQ.top();
PQ.pop();
if(dist[now.second]<now.first)continue;
for(auto nxt : adj[now.second]){
if(dist[now.second]+nxt.second<dist[nxt.first]){
dist[nxt.first]=dist[now.second]+nxt.second;
PQ.push({dist[nxt.first],nxt.first});
}
}
}
vector<pii> jarakb;
for(int i=1;i<=n;i++){
if(i==a || i==b || !special[i])continue;
jarakb.push_back({dist[i],i});
}
sort(jarakb.begin(),jarakb.end());
if(jaraka[0].second==jarakb[0].second)return min(jaraka[0].first+jarakb[1].first,jaraka[1].first+jarakb[0].first);
else return jaraka[0].first+jarakb[0].first;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int m,k,u,v,w;
cin >> n >> m >> k;
for(int i=1;i<=m;i++){
cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
for(int i=1;i<=k;i++){
cin >> u;
special[u]=true;
}
piii temp=MinimumPairSpecialDistance();
int kasussatu=temp.first,a=temp.second.first,b=temp.second.second;
skip[a]=true;skip[b]=true;
kasussatu+=MinimumPairSpecialDistance().first;
int ans=kasussatu;
ans=min(ans,kasusdua(a,b));
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
3 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2816 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
2 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
2 ms |
2688 KB |
Output is correct |
30 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
3 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2816 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
2 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
2 ms |
2688 KB |
Output is correct |
30 |
Correct |
3 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
2 ms |
2688 KB |
Output is correct |
33 |
Correct |
3 ms |
2688 KB |
Output is correct |
34 |
Correct |
2 ms |
2688 KB |
Output is correct |
35 |
Correct |
2 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
2816 KB |
Output is correct |
37 |
Correct |
5 ms |
2944 KB |
Output is correct |
38 |
Correct |
3 ms |
2688 KB |
Output is correct |
39 |
Correct |
28 ms |
5880 KB |
Output is correct |
40 |
Correct |
9 ms |
3456 KB |
Output is correct |
41 |
Correct |
3 ms |
2816 KB |
Output is correct |
42 |
Correct |
9 ms |
3456 KB |
Output is correct |
43 |
Correct |
4 ms |
2816 KB |
Output is correct |
44 |
Correct |
3 ms |
2688 KB |
Output is correct |
45 |
Correct |
2 ms |
2688 KB |
Output is correct |
46 |
Correct |
35 ms |
6136 KB |
Output is correct |
47 |
Correct |
7 ms |
3200 KB |
Output is correct |
48 |
Correct |
26 ms |
5760 KB |
Output is correct |
49 |
Correct |
28 ms |
5888 KB |
Output is correct |
50 |
Correct |
3 ms |
2688 KB |
Output is correct |
51 |
Correct |
3 ms |
2816 KB |
Output is correct |
52 |
Correct |
3 ms |
2688 KB |
Output is correct |
53 |
Correct |
17 ms |
4352 KB |
Output is correct |
54 |
Correct |
29 ms |
6008 KB |
Output is correct |
55 |
Correct |
3 ms |
2688 KB |
Output is correct |
56 |
Correct |
2 ms |
2688 KB |
Output is correct |
57 |
Correct |
3 ms |
2688 KB |
Output is correct |
58 |
Correct |
36 ms |
6264 KB |
Output is correct |
59 |
Correct |
2 ms |
2688 KB |
Output is correct |
60 |
Correct |
3 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
7400 KB |
Output is correct |
2 |
Correct |
9 ms |
3884 KB |
Output is correct |
3 |
Correct |
1663 ms |
101540 KB |
Output is correct |
4 |
Correct |
783 ms |
47672 KB |
Output is correct |
5 |
Correct |
208 ms |
12684 KB |
Output is correct |
6 |
Correct |
175 ms |
10500 KB |
Output is correct |
7 |
Correct |
234 ms |
13644 KB |
Output is correct |
8 |
Correct |
95 ms |
7544 KB |
Output is correct |
9 |
Correct |
152 ms |
10156 KB |
Output is correct |
10 |
Correct |
110 ms |
8340 KB |
Output is correct |
11 |
Correct |
1854 ms |
107348 KB |
Output is correct |
12 |
Correct |
141 ms |
8696 KB |
Output is correct |
13 |
Correct |
530 ms |
30884 KB |
Output is correct |
14 |
Correct |
262 ms |
13724 KB |
Output is correct |
15 |
Correct |
1804 ms |
104972 KB |
Output is correct |
16 |
Correct |
58 ms |
7032 KB |
Output is correct |
17 |
Correct |
1257 ms |
74508 KB |
Output is correct |
18 |
Correct |
8 ms |
4096 KB |
Output is correct |
19 |
Correct |
1971 ms |
112088 KB |
Output is correct |
20 |
Correct |
230 ms |
13004 KB |
Output is correct |
21 |
Correct |
197 ms |
11960 KB |
Output is correct |
22 |
Correct |
105 ms |
8232 KB |
Output is correct |
23 |
Correct |
29 ms |
6136 KB |
Output is correct |
24 |
Correct |
1306 ms |
82308 KB |
Output is correct |
25 |
Correct |
167 ms |
10292 KB |
Output is correct |
26 |
Correct |
88 ms |
7544 KB |
Output is correct |
27 |
Correct |
133 ms |
9184 KB |
Output is correct |
28 |
Correct |
18 ms |
5120 KB |
Output is correct |
29 |
Correct |
240 ms |
13428 KB |
Output is correct |
30 |
Correct |
466 ms |
23896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
3 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2816 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
2 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
2 ms |
2688 KB |
Output is correct |
30 |
Correct |
3 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
2 ms |
2688 KB |
Output is correct |
33 |
Correct |
3 ms |
2688 KB |
Output is correct |
34 |
Correct |
2 ms |
2688 KB |
Output is correct |
35 |
Correct |
2 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
2816 KB |
Output is correct |
37 |
Correct |
5 ms |
2944 KB |
Output is correct |
38 |
Correct |
3 ms |
2688 KB |
Output is correct |
39 |
Correct |
28 ms |
5880 KB |
Output is correct |
40 |
Correct |
9 ms |
3456 KB |
Output is correct |
41 |
Correct |
3 ms |
2816 KB |
Output is correct |
42 |
Correct |
9 ms |
3456 KB |
Output is correct |
43 |
Correct |
4 ms |
2816 KB |
Output is correct |
44 |
Correct |
3 ms |
2688 KB |
Output is correct |
45 |
Correct |
2 ms |
2688 KB |
Output is correct |
46 |
Correct |
35 ms |
6136 KB |
Output is correct |
47 |
Correct |
7 ms |
3200 KB |
Output is correct |
48 |
Correct |
26 ms |
5760 KB |
Output is correct |
49 |
Correct |
28 ms |
5888 KB |
Output is correct |
50 |
Correct |
3 ms |
2688 KB |
Output is correct |
51 |
Correct |
3 ms |
2816 KB |
Output is correct |
52 |
Correct |
3 ms |
2688 KB |
Output is correct |
53 |
Correct |
17 ms |
4352 KB |
Output is correct |
54 |
Correct |
29 ms |
6008 KB |
Output is correct |
55 |
Correct |
3 ms |
2688 KB |
Output is correct |
56 |
Correct |
2 ms |
2688 KB |
Output is correct |
57 |
Correct |
3 ms |
2688 KB |
Output is correct |
58 |
Correct |
36 ms |
6264 KB |
Output is correct |
59 |
Correct |
2 ms |
2688 KB |
Output is correct |
60 |
Correct |
3 ms |
2816 KB |
Output is correct |
61 |
Correct |
79 ms |
7400 KB |
Output is correct |
62 |
Correct |
9 ms |
3884 KB |
Output is correct |
63 |
Correct |
1663 ms |
101540 KB |
Output is correct |
64 |
Correct |
783 ms |
47672 KB |
Output is correct |
65 |
Correct |
208 ms |
12684 KB |
Output is correct |
66 |
Correct |
175 ms |
10500 KB |
Output is correct |
67 |
Correct |
234 ms |
13644 KB |
Output is correct |
68 |
Correct |
95 ms |
7544 KB |
Output is correct |
69 |
Correct |
152 ms |
10156 KB |
Output is correct |
70 |
Correct |
110 ms |
8340 KB |
Output is correct |
71 |
Correct |
1854 ms |
107348 KB |
Output is correct |
72 |
Correct |
141 ms |
8696 KB |
Output is correct |
73 |
Correct |
530 ms |
30884 KB |
Output is correct |
74 |
Correct |
262 ms |
13724 KB |
Output is correct |
75 |
Correct |
1804 ms |
104972 KB |
Output is correct |
76 |
Correct |
58 ms |
7032 KB |
Output is correct |
77 |
Correct |
1257 ms |
74508 KB |
Output is correct |
78 |
Correct |
8 ms |
4096 KB |
Output is correct |
79 |
Correct |
1971 ms |
112088 KB |
Output is correct |
80 |
Correct |
230 ms |
13004 KB |
Output is correct |
81 |
Correct |
197 ms |
11960 KB |
Output is correct |
82 |
Correct |
105 ms |
8232 KB |
Output is correct |
83 |
Correct |
29 ms |
6136 KB |
Output is correct |
84 |
Correct |
1306 ms |
82308 KB |
Output is correct |
85 |
Correct |
167 ms |
10292 KB |
Output is correct |
86 |
Correct |
88 ms |
7544 KB |
Output is correct |
87 |
Correct |
133 ms |
9184 KB |
Output is correct |
88 |
Correct |
18 ms |
5120 KB |
Output is correct |
89 |
Correct |
240 ms |
13428 KB |
Output is correct |
90 |
Correct |
466 ms |
23896 KB |
Output is correct |
91 |
Correct |
212 ms |
9604 KB |
Output is correct |
92 |
Correct |
2228 ms |
118676 KB |
Output is correct |
93 |
Correct |
351 ms |
13664 KB |
Output is correct |
94 |
Correct |
1449 ms |
69604 KB |
Output is correct |
95 |
Correct |
10 ms |
3840 KB |
Output is correct |
96 |
Correct |
11 ms |
4096 KB |
Output is correct |
97 |
Correct |
455 ms |
17760 KB |
Output is correct |
98 |
Correct |
229 ms |
9628 KB |
Output is correct |
99 |
Correct |
180 ms |
10392 KB |
Output is correct |
100 |
Correct |
2503 ms |
126816 KB |
Output is correct |
101 |
Correct |
1169 ms |
47368 KB |
Output is correct |
102 |
Correct |
1100 ms |
46604 KB |
Output is correct |
103 |
Correct |
1702 ms |
87000 KB |
Output is correct |
104 |
Correct |
2084 ms |
99880 KB |
Output is correct |
105 |
Correct |
129 ms |
7672 KB |
Output is correct |
106 |
Correct |
1719 ms |
87308 KB |
Output is correct |
107 |
Correct |
662 ms |
25624 KB |
Output is correct |
108 |
Correct |
330 ms |
12580 KB |
Output is correct |
109 |
Correct |
23 ms |
5504 KB |
Output is correct |
110 |
Correct |
126 ms |
7164 KB |
Output is correct |
111 |
Correct |
347 ms |
12860 KB |
Output is correct |
112 |
Correct |
2479 ms |
126980 KB |
Output is correct |
113 |
Correct |
2447 ms |
127784 KB |
Output is correct |
114 |
Correct |
330 ms |
12468 KB |
Output is correct |
115 |
Correct |
904 ms |
45276 KB |
Output is correct |
116 |
Correct |
2300 ms |
116328 KB |
Output is correct |
117 |
Correct |
1643 ms |
85088 KB |
Output is correct |
118 |
Correct |
1489 ms |
75540 KB |
Output is correct |
119 |
Correct |
2130 ms |
103692 KB |
Output is correct |
120 |
Correct |
1314 ms |
61460 KB |
Output is correct |