[Usaco2007 Feb]Cow Party奶牛派对
时间限制: 1 Sec 内存限制: 128 MB
题目描述
农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前去X牛棚参加派对还是返回住所,她们都采用了用时最少的路线.那么,用时最多的奶牛需要多少时间来回呢?
输入
第1行:三个用空格隔开的整数.
第2行到第M+1行,每行三个用空格隔开的整数:Ai, Bi,以及Ti.表示一条道路的起点,终点和需要花费的时间.
输出
唯一一行:一个整数: 所有参加聚会的奶牛中,需要花费总时间的最大值.
样例输入
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
样例输出
10
提示
共有4只奶牛参加聚会,有8条路,聚会位于第2个农场.
第4只奶牛可以直接到聚会所在地(花费3时间),然后返程路线经过第1和第3个农场(花费7时间),总共10时间.
邻接矩阵会超时QAQ
只好写邻接链表
(捂脸)写的好难看,有空改成函数吧还是…
var
w:array[0..200000,1..3]of longint;
p,dist:array[0..1000]of longint;
t:array[0..10000]of longint;
i,j,k,l:longint;
a,b,c,x,y,tt:longint;
n,m,start,finish,ans,sum,s:longint;
head,tail:longint;
function max(a,b:longint):longint;
begin
if a>b
then exit(a)
else exit(b);
end;
begin
readln(n,m,s); l:=n+1;
for i:=1 to m do
begin
readln(a,b,c);
w[l,1]:=b;
w[l,2]:=c;
if w[a,3]=0
then w[a,3]:=l
else w[w[a,1],3]:=l;
w[a,1]:=l;
inc(l);
end;
for k:=1 to n do
if k<>s
then
begin
sum:=0;
start:=k; finish:=s;
for i:=1 to n do
dist[i]:=100000;
dist[start]:=0;
head:=1; tail:=2; t[1]:=start; p[start]:=1;
while head<tail do
begin
x:=head; y:=tail;
for i:=head to tail-1 do
begin
tt:=w[t[i],3];
while tt<>0 do
begin
if dist[t[i]]+w[tt,2]<dist[w[tt,1]]
then
begin
dist[w[tt,1]]:=dist[t[i]]+w[tt,2];
if p[w[tt,1]]=0
then begin t[y]:=w[tt,1]; inc(y); end;
end;
tt:=w[tt,3];
end;
inc(x);
p[t[i]]:=0;
end;
head:=x; tail:=y;
end;
inc(sum,dist[finish]);
start:=s; finish:=k;
for i:=1 to n do
dist[i]:=100000;
dist[start]:=0;
head:=1; tail:=2; t[1]:=start; p[start]:=1;
while head<tail do
begin
x:=head; y:=tail;
for i:=head to tail-1 do
begin
tt:=w[t[i],3];
while tt<>0 do
begin
if dist[t[i]]+w[tt,2]<dist[w[tt,1]]
then
begin
dist[w[tt,1]]:=dist[t[i]]+w[tt,2];
if p[w[tt,1]]=0
then begin t[y]:=w[tt,1]; inc(y); end;
end;
tt:=w[tt,3];
end;
inc(x);
p[t[i]]:=0;
end;
head:=x; tail:=y;
end;
inc(sum,dist[finish]);
ans:=max(ans,sum);
end;
writeln(ans);
end.