#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;//find_by_order(ind);
//order_of_key()
//#define int long long
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define yes "YES"
#define no "NO"
#define bigg INT_MAX
#define debug(x) cout<<(#x)<<" = " <<x<<endl;
#define all(x) x.begin(),x.end()
#define sz size()
#define nn '\n'
#define mms(x,y) memset(x,y,sizeof(x))
#define forr(i,j,n) for (int i=j; i<n; i++)
#define forn(i,j,n) for (int i=j; i>n; i--)
#define fi first
#define se second
#define la "LA"
#define cinn(x,y) for(int i=0; i<y; i++) cin>>x[i];
#define pii pair<int,int>
int par[100005];
bool b[100005];
int fin(int x)
{
if (b[par[x]]) return par[x];
return par[x]=fin(x);
}
signed main()
{
// #ifndef LOCAL
// freopen("lifeguards.in","r",stdin);
// freopen("lifeguards.out","w", stdout);
// #endif
fast
int n,m,k;
cin>>n >>m >>k;
vector<pii> a[n+1];
forr(i,0,m)
{
int x,y,z;
cin>>x >>y >>z;
a[x].pb({y,z});
a[y].pb({x,z});
}
int s[k];
priority_queue<array<int,3>> q;
forr(i,0,k)
{
cin>>s[i];
q.push({0,s[i],s[i]});
b[s[i]]=1;
par[s[i]]=s[i];
}
//set <pii>vis;
set<int> vis[n+1];
int ans1=0;
int a1=0, a2=0;
while(!q.empty())
{
int c=q.top()[0], x=q.top()[1], last=q.top()[2];
q.pop();
if (vis[x].find(fin(last))!=vis[x].end()) continue;
par[x]=fin(last);
// cout<<c<<' '<<x<<endl;
if (b[x] && c!=0)
{
// cout<<x<<' '<<c<<endl;
ans1=-c;
a1=x;
a2=par[x];
break;
}
// if (!b[x])
vis[x].insert(par[x]);
// cout<<"PUT"<<x<<' '<<last<<endl;
for(auto y:a[x])
{
// cout"DWAR"<<y.fi<<' '<<par[x]<<endl;
if (vis[y.fi].find(par[x])==vis[y.fi].end() && par[x]!=y.fi)
{
int o=abs(c);
o+=y.se;
q.push({-o,y.fi,par[x]});
// cout<<yes<<y.fi<<' '<<y.se<<' '<<last<<endl;
}
}
}
/////
///new1
while(!q.empty()) q.pop();
// q.clear();
forr(i,1,n+1) vis[i].clear();
mms(par,0);
forr(i,0,k)
{
//cin>>s[i];
if (s[i]==a1 || s[i]==a2) continue;
q.push({0,s[i],s[i]});
// b[s[i]]=1;
par[s[i]]=s[i];
// last[s[i]]=s[i];
}
///////////////
while(!q.empty())
{
int c=q.top()[0], x=q.top()[1], last=q.top()[2];
q.pop();
if (vis[x].find(fin(last))!=vis[x].end()) continue;
par[x]=fin(last);
// cout<<c<<' '<<x<<endl;
if (b[x] && c!=0)
{
// cout<<x<<' '<<c<<endl;
ans1+=(-c);
// a1=x;
// a2=par[x];
break;
}
// if (!b[x])
vis[x].insert(par[x]);
// cout<<"PUT"<<x<<' '<<last<<endl;
for(auto y:a[x])
{
// cout"DWAR"<<y.fi<<' '<<par[x]<<endl;
if (vis[y.fi].find(par[x])==vis[y.fi].end() && par[x]!=y.fi && y.fi!=a1 && y.fi!=a2)
{
int o=abs(c);
o+=y.se;
q.push({-o,y.fi,par[x]});
// cout<<yes<<y.fi<<' '<<y.se<<' '<<last<<endl;
}
}
}
/////////////////
///ans1 done
/////
///new2
while(!q.empty()) q.pop();
// q.clear();
forr(i,1,n+1) vis[i].clear();
mms(par,0);
forr(i,0,k)
{
//cin>>s[i];
if (s[i]==a1) continue;
q.push({0,s[i],s[i]});
// b[s[i]]=1;
par[s[i]]=s[i];
// last[s[i]]=s[i];
}
///////////////
int ans2=0;
int b1=0, b2=0;
while(!q.empty())
{
int c=q.top()[0], x=q.top()[1], last=q.top()[2];
q.pop();
if (vis[x].find(fin(last))!=vis[x].end()) continue;
par[x]=fin(last);
// cout<<c<<' '<<x<<endl;
if (b[x] && c!=0)
{
// cout<<x<<' '<<c<<endl;
ans2=(-c);
b1=x;
b2=par[x];
break;
}
// if (!b[x])
vis[x].insert(par[x]);
// cout<<"PUT"<<x<<' '<<last<<endl;
for(auto y:a[x])
{
// cout"DWAR"<<y.fi<<' '<<par[x]<<endl;
if (vis[y.fi].find(par[x])==vis[y.fi].end() && par[x]!=y.fi && y.fi!=a1)
{
int o=abs(c);
o+=y.se;
q.push({-o,y.fi,par[x]});
// cout<<yes<<y.fi<<' '<<y.se<<' '<<last<<endl;
}
}
}
/////////////////
//new22
while(!q.empty()) q.pop();
// q.clear();
forr(i,1,n+1) vis[i].clear();
mms(par,0);
forr(i,0,k)
{
//cin>>s[i];
if (s[i]==a1) continue;
q.push({0,s[i],s[i]});
// b[s[i]]=1;
par[s[i]]=s[i];
// last[s[i]]=s[i];
}
///////////////
// int ans2=0;
//int b1=0, b2=0;
while(!q.empty()) q.pop();
// q.clear();
forr(i,1,n+1) vis[i].clear();
mms(par,0);
forr(i,0,k)
{
//cin>>s[i];
if (s[i]==b1 || s[i]==b2) continue;
q.push({0,s[i],s[i]});
// b[s[i]]=1;
par[s[i]]=s[i];
// last[s[i]]=s[i];
}
while(!q.empty())
{
int c=q.top()[0], x=q.top()[1], last=q.top()[2];
q.pop();
if (vis[x].find(fin(last))!=vis[x].end()) continue;
par[x]=fin(last);
// cout<<c<<' '<<x<<endl;
if (b[x] && c!=0)
{
// cout<<x<<' '<<c<<endl;
ans2+=(-c);
// b1=x;
// b2=par[x];
break;
}
// if (!b[x])
vis[x].insert(par[x]);
// cout<<"PUT"<<x<<' '<<last<<endl;
for(auto y:a[x])
{
// cout"DWAR"<<y.fi<<' '<<par[x]<<endl;
if (vis[y.fi].find(par[x])==vis[y.fi].end() && par[x]!=y.fi && y.fi!=b1 && y.fi!=b2)
{
int o=abs(c);
o+=y.se;
q.push({-o,y.fi,par[x]});
// cout<<yes<<y.fi<<' '<<y.se<<' '<<last<<endl;
}
}
}
///ans2 done
cout<<min(ans1,ans2);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
0 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
0 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
688 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
3 ms |
1116 KB |
Output is correct |
37 |
Correct |
9 ms |
1308 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
23 ms |
3336 KB |
Output is correct |
40 |
Correct |
13 ms |
1628 KB |
Output is correct |
41 |
Correct |
1 ms |
860 KB |
Output is correct |
42 |
Correct |
16 ms |
1624 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
1 ms |
856 KB |
Output is correct |
45 |
Correct |
1 ms |
860 KB |
Output is correct |
46 |
Correct |
114 ms |
6456 KB |
Output is correct |
47 |
Correct |
9 ms |
1304 KB |
Output is correct |
48 |
Correct |
30 ms |
3408 KB |
Output is correct |
49 |
Correct |
72 ms |
4316 KB |
Output is correct |
50 |
Correct |
1 ms |
856 KB |
Output is correct |
51 |
Correct |
1 ms |
856 KB |
Output is correct |
52 |
Correct |
1 ms |
860 KB |
Output is correct |
53 |
Correct |
14 ms |
2276 KB |
Output is correct |
54 |
Correct |
26 ms |
3400 KB |
Output is correct |
55 |
Correct |
1 ms |
860 KB |
Output is correct |
56 |
Correct |
1 ms |
1112 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
58 |
Correct |
62 ms |
4308 KB |
Output is correct |
59 |
Correct |
1 ms |
856 KB |
Output is correct |
60 |
Correct |
2 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
10580 KB |
Output is correct |
2 |
Correct |
9 ms |
8280 KB |
Output is correct |
3 |
Correct |
4737 ms |
117092 KB |
Output is correct |
4 |
Correct |
1189 ms |
48576 KB |
Output is correct |
5 |
Correct |
145 ms |
17248 KB |
Output is correct |
6 |
Correct |
48 ms |
12608 KB |
Output is correct |
7 |
Correct |
203 ms |
19952 KB |
Output is correct |
8 |
Correct |
32 ms |
10832 KB |
Output is correct |
9 |
Correct |
56 ms |
12652 KB |
Output is correct |
10 |
Correct |
38 ms |
11280 KB |
Output is correct |
11 |
Correct |
4035 ms |
118824 KB |
Output is correct |
12 |
Correct |
39 ms |
11600 KB |
Output is correct |
13 |
Correct |
1085 ms |
38380 KB |
Output is correct |
14 |
Correct |
92 ms |
14924 KB |
Output is correct |
15 |
Correct |
4048 ms |
117284 KB |
Output is correct |
16 |
Correct |
39 ms |
11484 KB |
Output is correct |
17 |
Correct |
895 ms |
64776 KB |
Output is correct |
18 |
Correct |
8 ms |
8284 KB |
Output is correct |
19 |
Correct |
1332 ms |
81856 KB |
Output is correct |
20 |
Correct |
97 ms |
14924 KB |
Output is correct |
21 |
Correct |
55 ms |
13148 KB |
Output is correct |
22 |
Correct |
31 ms |
11276 KB |
Output is correct |
23 |
Correct |
18 ms |
9820 KB |
Output is correct |
24 |
Correct |
3820 ms |
108544 KB |
Output is correct |
25 |
Correct |
39 ms |
12116 KB |
Output is correct |
26 |
Correct |
30 ms |
10844 KB |
Output is correct |
27 |
Correct |
38 ms |
11432 KB |
Output is correct |
28 |
Correct |
13 ms |
9052 KB |
Output is correct |
29 |
Correct |
66 ms |
13740 KB |
Output is correct |
30 |
Correct |
194 ms |
20800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
0 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
688 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
3 ms |
1116 KB |
Output is correct |
37 |
Correct |
9 ms |
1308 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
23 ms |
3336 KB |
Output is correct |
40 |
Correct |
13 ms |
1628 KB |
Output is correct |
41 |
Correct |
1 ms |
860 KB |
Output is correct |
42 |
Correct |
16 ms |
1624 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
1 ms |
856 KB |
Output is correct |
45 |
Correct |
1 ms |
860 KB |
Output is correct |
46 |
Correct |
114 ms |
6456 KB |
Output is correct |
47 |
Correct |
9 ms |
1304 KB |
Output is correct |
48 |
Correct |
30 ms |
3408 KB |
Output is correct |
49 |
Correct |
72 ms |
4316 KB |
Output is correct |
50 |
Correct |
1 ms |
856 KB |
Output is correct |
51 |
Correct |
1 ms |
856 KB |
Output is correct |
52 |
Correct |
1 ms |
860 KB |
Output is correct |
53 |
Correct |
14 ms |
2276 KB |
Output is correct |
54 |
Correct |
26 ms |
3400 KB |
Output is correct |
55 |
Correct |
1 ms |
860 KB |
Output is correct |
56 |
Correct |
1 ms |
1112 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
58 |
Correct |
62 ms |
4308 KB |
Output is correct |
59 |
Correct |
1 ms |
856 KB |
Output is correct |
60 |
Correct |
2 ms |
856 KB |
Output is correct |
61 |
Correct |
25 ms |
10580 KB |
Output is correct |
62 |
Correct |
9 ms |
8280 KB |
Output is correct |
63 |
Correct |
4737 ms |
117092 KB |
Output is correct |
64 |
Correct |
1189 ms |
48576 KB |
Output is correct |
65 |
Correct |
145 ms |
17248 KB |
Output is correct |
66 |
Correct |
48 ms |
12608 KB |
Output is correct |
67 |
Correct |
203 ms |
19952 KB |
Output is correct |
68 |
Correct |
32 ms |
10832 KB |
Output is correct |
69 |
Correct |
56 ms |
12652 KB |
Output is correct |
70 |
Correct |
38 ms |
11280 KB |
Output is correct |
71 |
Correct |
4035 ms |
118824 KB |
Output is correct |
72 |
Correct |
39 ms |
11600 KB |
Output is correct |
73 |
Correct |
1085 ms |
38380 KB |
Output is correct |
74 |
Correct |
92 ms |
14924 KB |
Output is correct |
75 |
Correct |
4048 ms |
117284 KB |
Output is correct |
76 |
Correct |
39 ms |
11484 KB |
Output is correct |
77 |
Correct |
895 ms |
64776 KB |
Output is correct |
78 |
Correct |
8 ms |
8284 KB |
Output is correct |
79 |
Correct |
1332 ms |
81856 KB |
Output is correct |
80 |
Correct |
97 ms |
14924 KB |
Output is correct |
81 |
Correct |
55 ms |
13148 KB |
Output is correct |
82 |
Correct |
31 ms |
11276 KB |
Output is correct |
83 |
Correct |
18 ms |
9820 KB |
Output is correct |
84 |
Correct |
3820 ms |
108544 KB |
Output is correct |
85 |
Correct |
39 ms |
12116 KB |
Output is correct |
86 |
Correct |
30 ms |
10844 KB |
Output is correct |
87 |
Correct |
38 ms |
11432 KB |
Output is correct |
88 |
Correct |
13 ms |
9052 KB |
Output is correct |
89 |
Correct |
66 ms |
13740 KB |
Output is correct |
90 |
Correct |
194 ms |
20800 KB |
Output is correct |
91 |
Correct |
79 ms |
15500 KB |
Output is correct |
92 |
Correct |
5538 ms |
168012 KB |
Output is correct |
93 |
Correct |
77 ms |
17440 KB |
Output is correct |
94 |
Correct |
497 ms |
71144 KB |
Output is correct |
95 |
Correct |
8 ms |
8540 KB |
Output is correct |
96 |
Correct |
9 ms |
8664 KB |
Output is correct |
97 |
Correct |
248 ms |
23884 KB |
Output is correct |
98 |
Correct |
46 ms |
13916 KB |
Output is correct |
99 |
Correct |
58 ms |
14480 KB |
Output is correct |
100 |
Correct |
2505 ms |
149916 KB |
Output is correct |
101 |
Correct |
348 ms |
48884 KB |
Output is correct |
102 |
Correct |
568 ms |
54420 KB |
Output is correct |
103 |
Correct |
4078 ms |
137672 KB |
Output is correct |
104 |
Correct |
1308 ms |
111624 KB |
Output is correct |
105 |
Correct |
42 ms |
12672 KB |
Output is correct |
106 |
Correct |
2782 ms |
139172 KB |
Output is correct |
107 |
Correct |
316 ms |
32148 KB |
Output is correct |
108 |
Correct |
63 ms |
16300 KB |
Output is correct |
109 |
Correct |
18 ms |
10008 KB |
Output is correct |
110 |
Correct |
26 ms |
11612 KB |
Output is correct |
111 |
Correct |
62 ms |
16372 KB |
Output is correct |
112 |
Correct |
3565 ms |
176672 KB |
Output is correct |
113 |
Correct |
4325 ms |
176852 KB |
Output is correct |
114 |
Correct |
184 ms |
19684 KB |
Output is correct |
115 |
Correct |
1925 ms |
71464 KB |
Output is correct |
116 |
Correct |
5189 ms |
165096 KB |
Output is correct |
117 |
Correct |
3507 ms |
136812 KB |
Output is correct |
118 |
Correct |
2508 ms |
101508 KB |
Output is correct |
119 |
Correct |
2108 ms |
127668 KB |
Output is correct |
120 |
Correct |
459 ms |
63396 KB |
Output is correct |