#include<bits/stdc++.h>
using namespace std;
#define N 100010
#define ll long long
#define ii pair<int,int>
#define fs first
#define sc second
#define pb push_back
#define iii pair<int,ii>
#define int ll
int n,m,k,ans=1e18,ktr[N],spec[N],check[N];
vector<ii>g[N];
ii dp[N][2];
priority_queue<ii>s;
iii b[N];
struct vet
{
int ktr[510]={},a[510][510];
ii dp[510][510];
void xuli()
{
memset(a,0x3f3f,sizeof(a));
for(int i=1;i<=n;i++)
a[i][i]=0;
for(int i=1;i<=m;i++)
{
int u,v,L;
cin>>u>>v>>L;
a[u][v]=min(a[u][v],L);
a[v][u]=min(a[v][u],L);
}
for(int i=1;i<=k;i++)
{
int u;
cin>>u;
ktr[u]=1;
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
// cout<<a[3][5]<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(ktr[i]==0||ktr[j]==0||(i==j))
dp[i][j]={1e9,j};
else dp[i][j]={a[i][j],j};
sort(dp[i]+1,dp[i]+1+n);
}
int ans=1e18;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int t=1;t<=min(5ll,n);t++)
for(int c=1;c<=min(5ll,n);c++)
if(i!=j&&i!=dp[i][t].sc&&i!=dp[j][c].sc&&j!=dp[i][t].sc&&j!=dp[j][c].sc&&dp[i][t].sc!=dp[j][c].sc)
{
ans=min(ans,dp[i][t].fs+dp[j][c].fs);
}
cout<<ans;
exit(0);
}
}vet;
void DFS(int u)
{
ktr[u]=1;
if(spec[u]==1)
{
dp[u][0]={0,u};
s.push({0,u});
}
for(auto v:g[u])
if(ktr[v.sc]==0)
{
DFS(v.sc);
}
}
void dijkstra()
{
while(!s.empty())
{
ii cc=s.top();
int u=cc.sc,L=-cc.fs;
s.pop();
if(check[u])
continue;
check[u]=1;
for(auto v:g[u])
{
if(dp[v.sc][0].fs>v.fs+L)
{
if(dp[v.sc][0].sc!=dp[u][0].sc)
dp[v.sc][1]=dp[v.sc][0];
dp[v.sc][0]={v.fs+L,dp[u][0].sc};
s.push({-dp[v.sc][0].fs,v.sc});
}else
{
if(dp[v.sc][0].sc!=dp[u][0].sc&&dp[v.sc][1].fs>v.fs+L)
{
dp[v.sc][1]={v.fs+L,dp[u][0].sc};
}
}
}
}
}
bool SS(const iii&a,const iii&b)
{
return a.sc<b.sc;
}
int ST[N*4];
void update(int id,int l,int r,int i,int val)
{
if(l>i||r<i)
return;
if(l==r)
{
ST[id]=min(ST[id],val);
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,i,val);
update(id*2+1,mid+1,r,i,val);
ST[id]=min(ST[id*2],ST[id*2+1]);
}
int get(int id,int l,int r,int u,int v)
{
if(l>v||r<u)
return 1e18;
if(l>=u&&r<=v)
return ST[id];
int mid=(l+r)/2;
return min(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
int32_t main()
{
// freopen("4special.inp","r",stdin);
// freopen("4special.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m>>k;
if(n<=500&&m<=500)
vet.xuli();
for(int i=1;i<=m;i++)
{
int u,v,L;
cin>>u>>v>>L;
g[u].pb({L,v});
g[v].pb({L,u});
}
for(int i=1;i<=k;i++)
{
int u;
cin>>u;
spec[u]=1;
}
for(int i=1;i<=n;i++)
dp[i][0]=dp[i][1]={1e18,0};
for(int i=1;i<=n;i++)
if(ktr[i]==0)
{
DFS(i);
dijkstra();
//cout<<"cc\n";
}
for(int i=1;i<=n;i++)
b[i]={dp[i][0].fs+dp[i][1].fs,{min(dp[i][0].sc,dp[i][1].sc),max(dp[i][0].sc,dp[i][1].sc)}};
sort(b+1,b+1+n,SS);
memset(ST,0x3f3f,sizeof(ST));
for(int i=2,pos=1;i<=n;i++)
{
if(b[i].sc.fs!=b[i-1].sc.fs)
{
while(pos<i)
{
if(b[pos].sc.fs!=b[pos].sc.sc)
update(1,1,n,b[pos].sc.sc,b[pos].fs);//,cout<<b[i].sc.sc<<" "<<b[i].fs<<endl;
pos++;
}
}
if(b[i].sc.fs!=b[i].sc.sc)
ans=min(ans,b[i].fs+min(get(1,1,n,1,b[i].sc.fs-1),min(get(1,1,n,b[i].sc.fs+1,b[i].sc.sc-1),get(1,1,n,b[i].sc.sc+1,n))));
// cout<<b[i].fs<<" "<<b[i].sc.fs<<" "<<b[i].sc.sc<<" "<<pos<<" "<<ans<<" "<<get(1,1,n,1,2)<<endl;
}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
6 ms |
9900 KB |
Output is correct |
3 |
Correct |
5 ms |
8712 KB |
Output is correct |
4 |
Correct |
5 ms |
8788 KB |
Output is correct |
5 |
Correct |
5 ms |
8788 KB |
Output is correct |
6 |
Correct |
5 ms |
8704 KB |
Output is correct |
7 |
Correct |
5 ms |
9940 KB |
Output is correct |
8 |
Correct |
6 ms |
9988 KB |
Output is correct |
9 |
Correct |
5 ms |
8788 KB |
Output is correct |
10 |
Correct |
5 ms |
8712 KB |
Output is correct |
11 |
Correct |
5 ms |
8788 KB |
Output is correct |
12 |
Correct |
5 ms |
8708 KB |
Output is correct |
13 |
Correct |
5 ms |
9940 KB |
Output is correct |
14 |
Correct |
5 ms |
9988 KB |
Output is correct |
15 |
Correct |
5 ms |
9940 KB |
Output is correct |
16 |
Correct |
5 ms |
8712 KB |
Output is correct |
17 |
Correct |
5 ms |
9984 KB |
Output is correct |
18 |
Correct |
5 ms |
8788 KB |
Output is correct |
19 |
Correct |
5 ms |
9988 KB |
Output is correct |
20 |
Correct |
5 ms |
8788 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
5 ms |
8708 KB |
Output is correct |
23 |
Correct |
4 ms |
8704 KB |
Output is correct |
24 |
Correct |
4 ms |
8788 KB |
Output is correct |
25 |
Correct |
5 ms |
8788 KB |
Output is correct |
26 |
Correct |
4 ms |
8784 KB |
Output is correct |
27 |
Correct |
4 ms |
8708 KB |
Output is correct |
28 |
Correct |
5 ms |
9940 KB |
Output is correct |
29 |
Correct |
5 ms |
9940 KB |
Output is correct |
30 |
Correct |
5 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
6 ms |
9900 KB |
Output is correct |
3 |
Correct |
5 ms |
8712 KB |
Output is correct |
4 |
Correct |
5 ms |
8788 KB |
Output is correct |
5 |
Correct |
5 ms |
8788 KB |
Output is correct |
6 |
Correct |
5 ms |
8704 KB |
Output is correct |
7 |
Correct |
5 ms |
9940 KB |
Output is correct |
8 |
Correct |
6 ms |
9988 KB |
Output is correct |
9 |
Correct |
5 ms |
8788 KB |
Output is correct |
10 |
Correct |
5 ms |
8712 KB |
Output is correct |
11 |
Correct |
5 ms |
8788 KB |
Output is correct |
12 |
Correct |
5 ms |
8708 KB |
Output is correct |
13 |
Correct |
5 ms |
9940 KB |
Output is correct |
14 |
Correct |
5 ms |
9988 KB |
Output is correct |
15 |
Correct |
5 ms |
9940 KB |
Output is correct |
16 |
Correct |
5 ms |
8712 KB |
Output is correct |
17 |
Correct |
5 ms |
9984 KB |
Output is correct |
18 |
Correct |
5 ms |
8788 KB |
Output is correct |
19 |
Correct |
5 ms |
9988 KB |
Output is correct |
20 |
Correct |
5 ms |
8788 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
5 ms |
8708 KB |
Output is correct |
23 |
Correct |
4 ms |
8704 KB |
Output is correct |
24 |
Correct |
4 ms |
8788 KB |
Output is correct |
25 |
Correct |
5 ms |
8788 KB |
Output is correct |
26 |
Correct |
4 ms |
8784 KB |
Output is correct |
27 |
Correct |
4 ms |
8708 KB |
Output is correct |
28 |
Correct |
5 ms |
9940 KB |
Output is correct |
29 |
Correct |
5 ms |
9940 KB |
Output is correct |
30 |
Correct |
5 ms |
9940 KB |
Output is correct |
31 |
Correct |
137 ms |
8788 KB |
Output is correct |
32 |
Correct |
5 ms |
9992 KB |
Output is correct |
33 |
Correct |
5 ms |
9940 KB |
Output is correct |
34 |
Correct |
137 ms |
8708 KB |
Output is correct |
35 |
Correct |
134 ms |
8796 KB |
Output is correct |
36 |
Correct |
6 ms |
10196 KB |
Output is correct |
37 |
Correct |
7 ms |
10324 KB |
Output is correct |
38 |
Correct |
5 ms |
9940 KB |
Output is correct |
39 |
Correct |
23 ms |
15452 KB |
Output is correct |
40 |
Correct |
9 ms |
11220 KB |
Output is correct |
41 |
Correct |
5 ms |
9940 KB |
Output is correct |
42 |
Correct |
9 ms |
11192 KB |
Output is correct |
43 |
Correct |
6 ms |
10184 KB |
Output is correct |
44 |
Correct |
5 ms |
9940 KB |
Output is correct |
45 |
Correct |
146 ms |
8796 KB |
Output is correct |
46 |
Correct |
27 ms |
15692 KB |
Output is correct |
47 |
Correct |
8 ms |
10768 KB |
Output is correct |
48 |
Correct |
22 ms |
15320 KB |
Output is correct |
49 |
Correct |
28 ms |
15368 KB |
Output is correct |
50 |
Correct |
5 ms |
9940 KB |
Output is correct |
51 |
Correct |
5 ms |
9940 KB |
Output is correct |
52 |
Correct |
5 ms |
9940 KB |
Output is correct |
53 |
Correct |
15 ms |
12812 KB |
Output is correct |
54 |
Correct |
28 ms |
15476 KB |
Output is correct |
55 |
Correct |
5 ms |
9992 KB |
Output is correct |
56 |
Correct |
142 ms |
8908 KB |
Output is correct |
57 |
Correct |
5 ms |
9940 KB |
Output is correct |
58 |
Correct |
29 ms |
15776 KB |
Output is correct |
59 |
Correct |
137 ms |
8788 KB |
Output is correct |
60 |
Correct |
6 ms |
10068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
22616 KB |
Output is correct |
2 |
Correct |
17 ms |
16988 KB |
Output is correct |
3 |
Correct |
1556 ms |
176820 KB |
Output is correct |
4 |
Correct |
732 ms |
90560 KB |
Output is correct |
5 |
Correct |
191 ms |
32704 KB |
Output is correct |
6 |
Correct |
170 ms |
29204 KB |
Output is correct |
7 |
Correct |
257 ms |
34588 KB |
Output is correct |
8 |
Correct |
102 ms |
22804 KB |
Output is correct |
9 |
Correct |
152 ms |
28080 KB |
Output is correct |
10 |
Correct |
124 ms |
24648 KB |
Output is correct |
11 |
Correct |
1361 ms |
184264 KB |
Output is correct |
12 |
Correct |
121 ms |
25536 KB |
Output is correct |
13 |
Correct |
496 ms |
63116 KB |
Output is correct |
14 |
Correct |
269 ms |
35488 KB |
Output is correct |
15 |
Correct |
1313 ms |
181004 KB |
Output is correct |
16 |
Correct |
68 ms |
21476 KB |
Output is correct |
17 |
Correct |
949 ms |
135160 KB |
Output is correct |
18 |
Correct |
17 ms |
17300 KB |
Output is correct |
19 |
Correct |
1417 ms |
189360 KB |
Output is correct |
20 |
Correct |
258 ms |
34072 KB |
Output is correct |
21 |
Correct |
188 ms |
31412 KB |
Output is correct |
22 |
Correct |
106 ms |
24280 KB |
Output is correct |
23 |
Correct |
34 ms |
20176 KB |
Output is correct |
24 |
Correct |
961 ms |
148112 KB |
Output is correct |
25 |
Correct |
152 ms |
28284 KB |
Output is correct |
26 |
Correct |
85 ms |
22840 KB |
Output is correct |
27 |
Correct |
143 ms |
26052 KB |
Output is correct |
28 |
Correct |
24 ms |
18568 KB |
Output is correct |
29 |
Correct |
209 ms |
34132 KB |
Output is correct |
30 |
Correct |
382 ms |
53048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8788 KB |
Output is correct |
2 |
Correct |
6 ms |
9900 KB |
Output is correct |
3 |
Correct |
5 ms |
8712 KB |
Output is correct |
4 |
Correct |
5 ms |
8788 KB |
Output is correct |
5 |
Correct |
5 ms |
8788 KB |
Output is correct |
6 |
Correct |
5 ms |
8704 KB |
Output is correct |
7 |
Correct |
5 ms |
9940 KB |
Output is correct |
8 |
Correct |
6 ms |
9988 KB |
Output is correct |
9 |
Correct |
5 ms |
8788 KB |
Output is correct |
10 |
Correct |
5 ms |
8712 KB |
Output is correct |
11 |
Correct |
5 ms |
8788 KB |
Output is correct |
12 |
Correct |
5 ms |
8708 KB |
Output is correct |
13 |
Correct |
5 ms |
9940 KB |
Output is correct |
14 |
Correct |
5 ms |
9988 KB |
Output is correct |
15 |
Correct |
5 ms |
9940 KB |
Output is correct |
16 |
Correct |
5 ms |
8712 KB |
Output is correct |
17 |
Correct |
5 ms |
9984 KB |
Output is correct |
18 |
Correct |
5 ms |
8788 KB |
Output is correct |
19 |
Correct |
5 ms |
9988 KB |
Output is correct |
20 |
Correct |
5 ms |
8788 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
5 ms |
8708 KB |
Output is correct |
23 |
Correct |
4 ms |
8704 KB |
Output is correct |
24 |
Correct |
4 ms |
8788 KB |
Output is correct |
25 |
Correct |
5 ms |
8788 KB |
Output is correct |
26 |
Correct |
4 ms |
8784 KB |
Output is correct |
27 |
Correct |
4 ms |
8708 KB |
Output is correct |
28 |
Correct |
5 ms |
9940 KB |
Output is correct |
29 |
Correct |
5 ms |
9940 KB |
Output is correct |
30 |
Correct |
5 ms |
9940 KB |
Output is correct |
31 |
Correct |
137 ms |
8788 KB |
Output is correct |
32 |
Correct |
5 ms |
9992 KB |
Output is correct |
33 |
Correct |
5 ms |
9940 KB |
Output is correct |
34 |
Correct |
137 ms |
8708 KB |
Output is correct |
35 |
Correct |
134 ms |
8796 KB |
Output is correct |
36 |
Correct |
6 ms |
10196 KB |
Output is correct |
37 |
Correct |
7 ms |
10324 KB |
Output is correct |
38 |
Correct |
5 ms |
9940 KB |
Output is correct |
39 |
Correct |
23 ms |
15452 KB |
Output is correct |
40 |
Correct |
9 ms |
11220 KB |
Output is correct |
41 |
Correct |
5 ms |
9940 KB |
Output is correct |
42 |
Correct |
9 ms |
11192 KB |
Output is correct |
43 |
Correct |
6 ms |
10184 KB |
Output is correct |
44 |
Correct |
5 ms |
9940 KB |
Output is correct |
45 |
Correct |
146 ms |
8796 KB |
Output is correct |
46 |
Correct |
27 ms |
15692 KB |
Output is correct |
47 |
Correct |
8 ms |
10768 KB |
Output is correct |
48 |
Correct |
22 ms |
15320 KB |
Output is correct |
49 |
Correct |
28 ms |
15368 KB |
Output is correct |
50 |
Correct |
5 ms |
9940 KB |
Output is correct |
51 |
Correct |
5 ms |
9940 KB |
Output is correct |
52 |
Correct |
5 ms |
9940 KB |
Output is correct |
53 |
Correct |
15 ms |
12812 KB |
Output is correct |
54 |
Correct |
28 ms |
15476 KB |
Output is correct |
55 |
Correct |
5 ms |
9992 KB |
Output is correct |
56 |
Correct |
142 ms |
8908 KB |
Output is correct |
57 |
Correct |
5 ms |
9940 KB |
Output is correct |
58 |
Correct |
29 ms |
15776 KB |
Output is correct |
59 |
Correct |
137 ms |
8788 KB |
Output is correct |
60 |
Correct |
6 ms |
10068 KB |
Output is correct |
61 |
Correct |
84 ms |
22616 KB |
Output is correct |
62 |
Correct |
17 ms |
16988 KB |
Output is correct |
63 |
Correct |
1556 ms |
176820 KB |
Output is correct |
64 |
Correct |
732 ms |
90560 KB |
Output is correct |
65 |
Correct |
191 ms |
32704 KB |
Output is correct |
66 |
Correct |
170 ms |
29204 KB |
Output is correct |
67 |
Correct |
257 ms |
34588 KB |
Output is correct |
68 |
Correct |
102 ms |
22804 KB |
Output is correct |
69 |
Correct |
152 ms |
28080 KB |
Output is correct |
70 |
Correct |
124 ms |
24648 KB |
Output is correct |
71 |
Correct |
1361 ms |
184264 KB |
Output is correct |
72 |
Correct |
121 ms |
25536 KB |
Output is correct |
73 |
Correct |
496 ms |
63116 KB |
Output is correct |
74 |
Correct |
269 ms |
35488 KB |
Output is correct |
75 |
Correct |
1313 ms |
181004 KB |
Output is correct |
76 |
Correct |
68 ms |
21476 KB |
Output is correct |
77 |
Correct |
949 ms |
135160 KB |
Output is correct |
78 |
Correct |
17 ms |
17300 KB |
Output is correct |
79 |
Correct |
1417 ms |
189360 KB |
Output is correct |
80 |
Correct |
258 ms |
34072 KB |
Output is correct |
81 |
Correct |
188 ms |
31412 KB |
Output is correct |
82 |
Correct |
106 ms |
24280 KB |
Output is correct |
83 |
Correct |
34 ms |
20176 KB |
Output is correct |
84 |
Correct |
961 ms |
148112 KB |
Output is correct |
85 |
Correct |
152 ms |
28284 KB |
Output is correct |
86 |
Correct |
85 ms |
22840 KB |
Output is correct |
87 |
Correct |
143 ms |
26052 KB |
Output is correct |
88 |
Correct |
24 ms |
18568 KB |
Output is correct |
89 |
Correct |
209 ms |
34132 KB |
Output is correct |
90 |
Correct |
382 ms |
53048 KB |
Output is correct |
91 |
Correct |
143 ms |
27100 KB |
Output is correct |
92 |
Correct |
1409 ms |
198332 KB |
Output is correct |
93 |
Correct |
226 ms |
34976 KB |
Output is correct |
94 |
Correct |
875 ms |
126700 KB |
Output is correct |
95 |
Correct |
16 ms |
16980 KB |
Output is correct |
96 |
Correct |
18 ms |
17356 KB |
Output is correct |
97 |
Correct |
304 ms |
41624 KB |
Output is correct |
98 |
Correct |
140 ms |
27020 KB |
Output is correct |
99 |
Correct |
176 ms |
28796 KB |
Output is correct |
100 |
Correct |
1520 ms |
214636 KB |
Output is correct |
101 |
Correct |
651 ms |
90572 KB |
Output is correct |
102 |
Correct |
631 ms |
89584 KB |
Output is correct |
103 |
Correct |
991 ms |
152796 KB |
Output is correct |
104 |
Correct |
1243 ms |
171716 KB |
Output is correct |
105 |
Correct |
97 ms |
23116 KB |
Output is correct |
106 |
Correct |
1085 ms |
154612 KB |
Output is correct |
107 |
Correct |
423 ms |
55996 KB |
Output is correct |
108 |
Correct |
210 ms |
32812 KB |
Output is correct |
109 |
Correct |
29 ms |
18828 KB |
Output is correct |
110 |
Correct |
75 ms |
22092 KB |
Output is correct |
111 |
Correct |
211 ms |
33464 KB |
Output is correct |
112 |
Correct |
1557 ms |
214252 KB |
Output is correct |
113 |
Correct |
1532 ms |
215164 KB |
Output is correct |
114 |
Correct |
193 ms |
32436 KB |
Output is correct |
115 |
Correct |
577 ms |
85032 KB |
Output is correct |
116 |
Correct |
1396 ms |
194340 KB |
Output is correct |
117 |
Correct |
1010 ms |
150088 KB |
Output is correct |
118 |
Correct |
914 ms |
135400 KB |
Output is correct |
119 |
Correct |
1265 ms |
176032 KB |
Output is correct |
120 |
Correct |
829 ms |
113044 KB |
Output is correct |