中山培训第四题 2016.7.9

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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值