题目描述
经过努力,LCJ终于获得了一个带薪假期。他准备要在N个城市中挑选若干个进行旅游,其中有K个城市他是一定要去的。然而他英(qi)明(guai)的上司KID向他提出了一个要求,因为经费的问题,他的旅行路线必须是某两个城市之间的一条最短路。现在LCJ就要在这N个城市之间的道路找到这样一条路线:它是一条某两个城市之间的最短路,经过了K个特殊的城市,在满足条件的路线中,找到最短的一条。
输入
第一行两个数N,M。表示有N座城市,M条边。
接下来M行每行三个数xi,yi,vi,表示有一条长度为vi的双向路径连接对应的两座城市。
接下来一个数K。
接下来一行K个数,表示一定要经过的城市。
输出
一个数,符合要求的最短最短路长度。
样例输入
6 6
1 2 2
2 6 2
1 3 1
3 4 1
4 5 1
5 6 1
3
5 1 3
6 6
1 2 2
2 6 2
1 3 1
3 4 1
4 5 1
5 6 1
2
1 6
样例输出
3
4
提示
【数据范围】
对于30%的数据,1<=N<=10,1<=M<=20
对于60%的数据,1<=N<=500,1<=M<=1000。
对于100%的数据,1<=N<=100000,1<=M<=300000,1<=vi<=10000,1<=K<=N,保证有解。
对于100%的数据,找到K个点中的任意一个点作为起点,求最短路。显然,另外K-1个点中距离这个点最远的点一定是答案路径中的一端。找到这个点,以它为起点再求一次最短路,再找一个最远点就是了,题目因为保证有解。
var
head,ret,next,len,dis,a:array[0..800000] of longint;
d:array[0..2000000] of longint;
flag:array[0..800000] of boolean;
i,j,n,m,u,v,w,max,tot,k:longint;
procedure ins(u,v,w:longint);
begin
tot:=tot+1;
ret[tot]:=v;
len[tot]:=w;
next[tot]:=head[u];
head[u]:=tot;
end;
procedure SPFA(u:longint);
var
i,h,tail,v,s:longint;
begin
for i:=1 to n do
begin
dis[i]:=maxlongint;
flag[i]:=false;
end;
h:=0;
tail:=1;
d[1]:=u;
dis[u]:=0;
flag[u]:=true;
while h<>tail do
begin
h:=h+1;
s:=d[h];
i:=head[s];
while i<>0 do
begin
v:=ret[i];
if dis[v]>dis[s]+len[i] then
begin
dis[v]:=dis[s]+len[i];
if flag[v]=false then
begin
flag[v]:=true;
tail:=tail+1;
d[tail]:=v;
end;
end;
i:=next[i];
end;
flag[s]:=false;
end;
end;
begin
readln(n,m);
for i:=1 to m do
begin
readln(u,v,w);
ins(u,v,w);
ins(v,u,w);
end;
readln(k);
for i:=1 to k do
read(a[i]);
SPFA(a[1]);
max:=0;
u:=a[1];
for i:=1 to k do
begin
if dis[a[i]]>max then begin max:=dis[a[i]]; u:=a[i]; end;
end;
SPFA(u);
max:=0;
for i:=1 to k do
if dis[a[i]]>max then max:=dis[a[i]];
write(max);
end.