#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN=100'000;
vector<pair<int,int>> adjlist[MAXN];
int spec[MAXN];
bool is_spec[MAXN];
pair<int,int> nearest[MAXN]; // dist, owner
int bnear[MAXN];
int main(){
ios_base::sync_with_stdio(false);
int n,m,k;
cin>>n>>m>>k;
for(int i=0;i<m;++i){
int u,v,w;
cin>>u>>v>>w;
--u;--v;
adjlist[u].emplace_back(v,w);
adjlist[v].emplace_back(u,w);
}
for(int i=0;i<k;++i){
cin>>spec[i];
--spec[i];
is_spec[spec[i]]=true;
}
int ansu, ansv, ansdist=numeric_limits<int>::max()>>1, ansother=numeric_limits<int>::max()>>1, ansu1, ansu1dist=numeric_limits<int>::max()>>1, ansu2dist=numeric_limits<int>::max()>>1, ansv1, ansv1dist=numeric_limits<int>::max()>>1, ansv2dist=numeric_limits<int>::max()>>1;
struct pq_item{
int index;
int dist;
int owner;
};
struct pq_item_comp{
bool operator()(const pq_item& a, const pq_item& b) const {
return a.dist>b.dist;
}
};
struct b_item{
int index;
int dist;
};
struct b_item_comp{
bool operator()(const b_item& a, const b_item& b) const {
return a.dist>b.dist;
}
};
{
fill(nearest,nearest+n,make_pair(-1,-1));
priority_queue<pq_item,vector<pq_item>,pq_item_comp> pq;
for(int i=0;i<k;++i){
pq.push({spec[i],0,spec[i]});
}
while(!pq.empty()){
pq_item tmp = pq.top();
pq.pop();
if(nearest[tmp.index].first!=-1){
if(nearest[tmp.index].second!=tmp.owner){
break;
}
continue;
}
nearest[tmp.index]={tmp.dist, tmp.owner};
for(const pair<int,int>& pr : adjlist[tmp.index]) {
if(nearest[pr.first].first==-1||nearest[pr.first].second!=tmp.owner){
int pdist;
if(nearest[pr.first].first!=-1&&(pdist=tmp.dist+pr.second+nearest[pr.first].first)<ansdist){
ansu=tmp.owner;
ansv=nearest[pr.first].second;
ansdist=pdist;
}
pq.push({pr.first,tmp.dist+pr.second,tmp.owner});
}
}
}
}
{
fill(nearest,nearest+n,make_pair(-1,-1));
priority_queue<pq_item,vector<pq_item>,pq_item_comp> pq;
for(int i=0;i<k;++i){
if(spec[i]!=ansu&&spec[i]!=ansv){
pq.push({spec[i],0,spec[i]});
}
}
while(!pq.empty()){
pq_item tmp = pq.top();
pq.pop();
if(nearest[tmp.index].first!=-1){
if(nearest[tmp.index].second!=tmp.owner){
break;
}
continue;
}
nearest[tmp.index]={tmp.dist, tmp.owner};
for(const pair<int,int>& pr : adjlist[tmp.index]) {
if(nearest[pr.first].first==-1||nearest[pr.first].second!=tmp.owner){
if(nearest[pr.first].first!=-1)ansother=min(ansother,tmp.dist+pr.second+nearest[pr.first].first);
pq.push({pr.first,tmp.dist+pr.second,tmp.owner});
}
}
}
}
{
fill(bnear,bnear+n,-1);
bool counter=false;
priority_queue<b_item,vector<b_item>,b_item_comp> pq;
pq.push({ansu,0});
while(!pq.empty()){
b_item tmp = pq.top();
pq.pop();
if(bnear[tmp.index]!=-1)continue;
if(is_spec[tmp.index]&&tmp.index!=ansu&&tmp.index!=ansv){
if(!counter){
ansu1=tmp.index;
ansu1dist=tmp.dist;
counter=true;
}
else{
ansu2dist=tmp.dist;
break;
}
}
bnear[tmp.index]=tmp.dist;
for(const pair<int,int>& pr : adjlist[tmp.index]) {
if(bnear[pr.first]==-1){
pq.push({pr.first,tmp.dist+pr.second});
}
}
}
}
{
fill(bnear,bnear+n,-1);
bool counter=false;
priority_queue<b_item,vector<b_item>,b_item_comp> pq;
pq.push({ansv,0});
while(!pq.empty()){
b_item tmp = pq.top();
pq.pop();
if(bnear[tmp.index]!=-1)continue;
if(is_spec[tmp.index]&&tmp.index!=ansu&&tmp.index!=ansv){
if(!counter){
ansv1=tmp.index;
ansv1dist=tmp.dist;
counter=true;
}
else{
ansv2dist=tmp.dist;
break;
}
}
bnear[tmp.index]=tmp.dist;
for(const pair<int,int>& pr : adjlist[tmp.index]) {
if(bnear[pr.first]==-1){
pq.push({pr.first,tmp.dist+pr.second});
}
}
}
}
cout<<min(ansdist+ansother, (ansu1!=ansv1) ? (ansu1dist+ansv1dist) : min(ansu1dist+ansv2dist, ansu2dist+ansv1dist));
//cout<<ansu<<' '<<ansv<<' '<<ansdist<<' '<<ansother<<' '<<ansu1<<' '<<ansu1dist<<' '<<ansu2dist<<' '<<ansv1<<' '<<ansv1dist<<' '<<ansv2dist<<endl;
}
Compilation message
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:109:51: warning: 'ansv' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(is_spec[tmp.index]&&tmp.index!=ansu&&tmp.index!=ansv){
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
RelayMarathon.cpp:109:34: warning: 'ansu' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(is_spec[tmp.index]&&tmp.index!=ansu&&tmp.index!=ansv){
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2816 KB |
Output is correct |
3 |
Correct |
3 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 |
2816 KB |
Output is correct |
14 |
Correct |
3 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 |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2816 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 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 |
2 ms |
2816 KB |
Output is correct |
29 |
Correct |
2 ms |
2816 KB |
Output is correct |
30 |
Correct |
2 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2816 KB |
Output is correct |
3 |
Correct |
3 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 |
2816 KB |
Output is correct |
14 |
Correct |
3 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 |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2816 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 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 |
2 ms |
2816 KB |
Output is correct |
29 |
Correct |
2 ms |
2816 KB |
Output is correct |
30 |
Correct |
2 ms |
2816 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
2 ms |
2688 KB |
Output is correct |
33 |
Correct |
2 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 |
3 ms |
2944 KB |
Output is correct |
37 |
Correct |
5 ms |
3252 KB |
Output is correct |
38 |
Correct |
3 ms |
2688 KB |
Output is correct |
39 |
Correct |
39 ms |
5440 KB |
Output is correct |
40 |
Correct |
9 ms |
3872 KB |
Output is correct |
41 |
Correct |
2 ms |
2688 KB |
Output is correct |
42 |
Correct |
9 ms |
3924 KB |
Output is correct |
43 |
Correct |
3 ms |
2944 KB |
Output is correct |
44 |
Correct |
2 ms |
2688 KB |
Output is correct |
45 |
Correct |
2 ms |
2688 KB |
Output is correct |
46 |
Correct |
53 ms |
9848 KB |
Output is correct |
47 |
Correct |
9 ms |
3348 KB |
Output is correct |
48 |
Correct |
27 ms |
6096 KB |
Output is correct |
49 |
Correct |
33 ms |
7448 KB |
Output is correct |
50 |
Correct |
2 ms |
2688 KB |
Output is correct |
51 |
Correct |
2 ms |
2944 KB |
Output is correct |
52 |
Correct |
3 ms |
2688 KB |
Output is correct |
53 |
Correct |
19 ms |
4148 KB |
Output is correct |
54 |
Correct |
29 ms |
5588 KB |
Output is correct |
55 |
Correct |
2 ms |
2688 KB |
Output is correct |
56 |
Correct |
2 ms |
2688 KB |
Output is correct |
57 |
Correct |
2 ms |
2688 KB |
Output is correct |
58 |
Correct |
43 ms |
7444 KB |
Output is correct |
59 |
Correct |
2 ms |
2688 KB |
Output is correct |
60 |
Correct |
3 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
6776 KB |
Output is correct |
2 |
Correct |
7 ms |
4352 KB |
Output is correct |
3 |
Correct |
2191 ms |
135388 KB |
Output is correct |
4 |
Correct |
799 ms |
54012 KB |
Output is correct |
5 |
Correct |
130 ms |
9468 KB |
Output is correct |
6 |
Correct |
113 ms |
8440 KB |
Output is correct |
7 |
Correct |
160 ms |
10132 KB |
Output is correct |
8 |
Correct |
51 ms |
6776 KB |
Output is correct |
9 |
Correct |
116 ms |
8136 KB |
Output is correct |
10 |
Correct |
78 ms |
7160 KB |
Output is correct |
11 |
Correct |
2478 ms |
137672 KB |
Output is correct |
12 |
Correct |
73 ms |
7392 KB |
Output is correct |
13 |
Correct |
655 ms |
43196 KB |
Output is correct |
14 |
Correct |
160 ms |
10904 KB |
Output is correct |
15 |
Correct |
2323 ms |
136588 KB |
Output is correct |
16 |
Correct |
50 ms |
6392 KB |
Output is correct |
17 |
Correct |
1193 ms |
65188 KB |
Output is correct |
18 |
Correct |
7 ms |
4480 KB |
Output is correct |
19 |
Correct |
1826 ms |
84444 KB |
Output is correct |
20 |
Correct |
158 ms |
10616 KB |
Output is correct |
21 |
Correct |
181 ms |
9128 KB |
Output is correct |
22 |
Correct |
69 ms |
7088 KB |
Output is correct |
23 |
Correct |
40 ms |
5880 KB |
Output is correct |
24 |
Correct |
1736 ms |
126724 KB |
Output is correct |
25 |
Correct |
93 ms |
8184 KB |
Output is correct |
26 |
Correct |
55 ms |
6660 KB |
Output is correct |
27 |
Correct |
77 ms |
7544 KB |
Output is correct |
28 |
Correct |
20 ms |
5120 KB |
Output is correct |
29 |
Correct |
134 ms |
9972 KB |
Output is correct |
30 |
Correct |
331 ms |
16816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2816 KB |
Output is correct |
3 |
Correct |
3 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 |
2816 KB |
Output is correct |
14 |
Correct |
3 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 |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2816 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 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 |
2 ms |
2816 KB |
Output is correct |
29 |
Correct |
2 ms |
2816 KB |
Output is correct |
30 |
Correct |
2 ms |
2816 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
2 ms |
2688 KB |
Output is correct |
33 |
Correct |
2 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 |
3 ms |
2944 KB |
Output is correct |
37 |
Correct |
5 ms |
3252 KB |
Output is correct |
38 |
Correct |
3 ms |
2688 KB |
Output is correct |
39 |
Correct |
39 ms |
5440 KB |
Output is correct |
40 |
Correct |
9 ms |
3872 KB |
Output is correct |
41 |
Correct |
2 ms |
2688 KB |
Output is correct |
42 |
Correct |
9 ms |
3924 KB |
Output is correct |
43 |
Correct |
3 ms |
2944 KB |
Output is correct |
44 |
Correct |
2 ms |
2688 KB |
Output is correct |
45 |
Correct |
2 ms |
2688 KB |
Output is correct |
46 |
Correct |
53 ms |
9848 KB |
Output is correct |
47 |
Correct |
9 ms |
3348 KB |
Output is correct |
48 |
Correct |
27 ms |
6096 KB |
Output is correct |
49 |
Correct |
33 ms |
7448 KB |
Output is correct |
50 |
Correct |
2 ms |
2688 KB |
Output is correct |
51 |
Correct |
2 ms |
2944 KB |
Output is correct |
52 |
Correct |
3 ms |
2688 KB |
Output is correct |
53 |
Correct |
19 ms |
4148 KB |
Output is correct |
54 |
Correct |
29 ms |
5588 KB |
Output is correct |
55 |
Correct |
2 ms |
2688 KB |
Output is correct |
56 |
Correct |
2 ms |
2688 KB |
Output is correct |
57 |
Correct |
2 ms |
2688 KB |
Output is correct |
58 |
Correct |
43 ms |
7444 KB |
Output is correct |
59 |
Correct |
2 ms |
2688 KB |
Output is correct |
60 |
Correct |
3 ms |
2816 KB |
Output is correct |
61 |
Correct |
45 ms |
6776 KB |
Output is correct |
62 |
Correct |
7 ms |
4352 KB |
Output is correct |
63 |
Correct |
2191 ms |
135388 KB |
Output is correct |
64 |
Correct |
799 ms |
54012 KB |
Output is correct |
65 |
Correct |
130 ms |
9468 KB |
Output is correct |
66 |
Correct |
113 ms |
8440 KB |
Output is correct |
67 |
Correct |
160 ms |
10132 KB |
Output is correct |
68 |
Correct |
51 ms |
6776 KB |
Output is correct |
69 |
Correct |
116 ms |
8136 KB |
Output is correct |
70 |
Correct |
78 ms |
7160 KB |
Output is correct |
71 |
Correct |
2478 ms |
137672 KB |
Output is correct |
72 |
Correct |
73 ms |
7392 KB |
Output is correct |
73 |
Correct |
655 ms |
43196 KB |
Output is correct |
74 |
Correct |
160 ms |
10904 KB |
Output is correct |
75 |
Correct |
2323 ms |
136588 KB |
Output is correct |
76 |
Correct |
50 ms |
6392 KB |
Output is correct |
77 |
Correct |
1193 ms |
65188 KB |
Output is correct |
78 |
Correct |
7 ms |
4480 KB |
Output is correct |
79 |
Correct |
1826 ms |
84444 KB |
Output is correct |
80 |
Correct |
158 ms |
10616 KB |
Output is correct |
81 |
Correct |
181 ms |
9128 KB |
Output is correct |
82 |
Correct |
69 ms |
7088 KB |
Output is correct |
83 |
Correct |
40 ms |
5880 KB |
Output is correct |
84 |
Correct |
1736 ms |
126724 KB |
Output is correct |
85 |
Correct |
93 ms |
8184 KB |
Output is correct |
86 |
Correct |
55 ms |
6660 KB |
Output is correct |
87 |
Correct |
77 ms |
7544 KB |
Output is correct |
88 |
Correct |
20 ms |
5120 KB |
Output is correct |
89 |
Correct |
134 ms |
9972 KB |
Output is correct |
90 |
Correct |
331 ms |
16816 KB |
Output is correct |
91 |
Correct |
124 ms |
8212 KB |
Output is correct |
92 |
Correct |
2468 ms |
143552 KB |
Output is correct |
93 |
Correct |
155 ms |
10296 KB |
Output is correct |
94 |
Correct |
1020 ms |
44224 KB |
Output is correct |
95 |
Correct |
9 ms |
4480 KB |
Output is correct |
96 |
Correct |
9 ms |
4608 KB |
Output is correct |
97 |
Correct |
373 ms |
16896 KB |
Output is correct |
98 |
Correct |
120 ms |
7928 KB |
Output is correct |
99 |
Correct |
93 ms |
8312 KB |
Output is correct |
100 |
Correct |
1878 ms |
121176 KB |
Output is correct |
101 |
Correct |
810 ms |
30052 KB |
Output is correct |
102 |
Correct |
613 ms |
39052 KB |
Output is correct |
103 |
Correct |
1676 ms |
128616 KB |
Output is correct |
104 |
Correct |
2116 ms |
80728 KB |
Output is correct |
105 |
Correct |
61 ms |
6776 KB |
Output is correct |
106 |
Correct |
1532 ms |
129008 KB |
Output is correct |
107 |
Correct |
350 ms |
21884 KB |
Output is correct |
108 |
Correct |
110 ms |
9592 KB |
Output is correct |
109 |
Correct |
19 ms |
5504 KB |
Output is correct |
110 |
Correct |
37 ms |
6520 KB |
Output is correct |
111 |
Correct |
194 ms |
9976 KB |
Output is correct |
112 |
Correct |
2265 ms |
149940 KB |
Output is correct |
113 |
Correct |
2310 ms |
150568 KB |
Output is correct |
114 |
Correct |
155 ms |
10856 KB |
Output is correct |
115 |
Correct |
850 ms |
71808 KB |
Output is correct |
116 |
Correct |
2247 ms |
141936 KB |
Output is correct |
117 |
Correct |
1558 ms |
127420 KB |
Output is correct |
118 |
Correct |
1357 ms |
95080 KB |
Output is correct |
119 |
Correct |
1803 ms |
103960 KB |
Output is correct |
120 |
Correct |
954 ms |
38684 KB |
Output is correct |