中山培训第四题 2016.7.9

来源:互联网 发布:剑三重置版优化补丁 编辑:程序博客网 时间:2024/04/29 12:03

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

 


const

 MaxE=100000;

 MaxV=2000000;

 

type

 rec=record

       x,y,w,next:longint;

     end;

var

 n,m,c,q,i,x,y,w:longint;

 g,g1:array [1..Maxv] of rec;

  ls,ls1:array [1..Maxe] of longint;

 v,d,list,s:array [1..maxe] of longint;

 

procedure spfa;

var

 head,tail,t,i:longint;

begin

 head:=0; tail:=1;

 list[1]:=c;

  fori:=1 to n do

 d[i]:=maxlongint div 3;

 d[c]:=0;

 v[c]:=1;

  whilehead<>tail do

    begin

     head:=head mod maxe+1;

     t:=ls[list[head]];

     while t>0 do

       with g[t] do

         begin

           if d[x]+w<d[y] then

              begin

                d[y]:=d[x]+w;

                if v[y]=0 then

                  begin

                    v[y]:=1;

                    tail:=tail mod maxe+1;

                    list[tail]:=y;

                  end;

              end;

           t:=next;

         end;

     v[list[head]]:=0;

    end;

end;

 

procedure print;

 vari:longint;

     max:int64;

begin

 max:=0;

 fori:=1 to n do

  ifs[i]+d[i]>max then max:=s[i]+d[i];

 write(max);

end;

begin

   read(n,m,c);

  fori:=1 to n do ls[i]:=0;

  fori:=1 to m do

   begin

    read(x,y,w);

   g[i].x:=x;

   g[i].y:=y;

   g[i].w:=w;

    g[i].next:=ls[x];

   ls[x]:=i;

   g1[i].x:=y;

   g1[i].y:=x;

   g1[i].w:=w;

   g1[i].next:=ls1[y];

   ls1[y]:=i;

   end;

 

 spfa;

s:=d;

g:=g1;

ls:=ls1;

spfa;

print;

end.

 

0 0
原创粉丝点击