Description
N头牛要去参加一场在编号为x(1<=x<=n)的牛的农场举行的派对(1<=N<=1000),有M(1<=m<=100000)条有向道路,每条路长ti(1<=ti<=100);
每头牛都必须参加完派对后回到家,每头牛都会选择最短路径,求这n个牛的最短路径(一个来回)中最长的一条的长度。
特别提醒:可能有权值不同的重边。
Input
第1行: N,M,X;
第2~m+1行: Ai,Bi,Ti,表示有一条从Ai到Bi的路,长度为Ti.
Output
最长最短路的长度。
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
思路
作为苦逼的oi选手要有一双慧眼比如我这样
求单源最短路径优先考虑spfa,对于回来的路径只要把所有边反向,一来一回相加求最大值
身在中山的第一套模拟,被完虐,心碎碎/(ㄒoㄒ)/~~
写的时候数组太小就挂了
代码/pas
type
rec=record
x,y,w,next:longint;
end;
var
n,m,p,maxE,inf:longint;
e,g:array[1..200000]of rec;
d,ls,ans,state:array[1..2000]of longint;
v:array[1..2000]of boolean;
min:int64;
procedure add(x,y,w:Longint);
begin
inc(maxE);
g[maxE].x:=x;
g[maxE].y:=y;
g[maxE].w:=w;
g[maxE].next:=ls[x];
ls[x]:=maxE;
end;
procedure addd(x,y,w:longint);
begin
inc(maxE);
e[maxE].x:=x;
e[maxE].y:=y;
e[maxE].w:=w;
e[maxE].next:=ls[x];
ls[x]:=maxE;
end;
procedure spfa;
var
head,tail,t:longint;
begin
head:=0;
tail:=1;
fillchar(state,sizeof(state),0);
fillchar(v,sizeof(v),false);
fillchar(d,sizeof(d),$7f);
inf:=d[1];
d[p]:=0;
v[p]:=true;
state[1]:=p;
repeat
inc(head);
t:=ls[state[head]];
while t>0 do
with g[t] do
begin
if d[x]+w<d[y] then
begin
d[y]:=d[x]+w;
if not v[y] then
begin
v[y]:=true;
inc(tail);
state[tail]:=y;
end;
end;
t:=next;
end;
v[state[head]]:=false;
until head>=tail;
for t:=1 to n do
inc(ans[t],d[t]);
end;
procedure main;
var
i,max:longint;
begin
spfa;
fillchar(ls,sizeof(ls),0);
for i:=1 to maxE do
with g[i] do addd(y,x,w);
g:=e;
spfa;
max:=0;
for i:=1 to n do
if (ans[i]>max)and(ans[i]<inf) then max:=ans[i];
writeln(max);
end;
procedure init;
var
i,x,y,w:longint;
begin
readln(n,m,p);
fillchar(ls,sizeof(ls),0);
maxE:=0;
for i:=1 to m do
begin
readln(x,y,w);
add(x,y,w);
end;
end;
begin
init;
main;
end.