TYVJ1356(腾讯大战360)

算法:最短路

求两遍最短路,一个是从起点走向终点,另一个从终点走向起点,记录下每次走每个点的最短路,然后枚举一遍,对于每个点,找一个最大的最短路看能否更新min。

program P1356;

const
     maxn=10000;
     
type
    atp=record
              y,dis,next:longint;
    end;

var
   head,tail,n,m,tot,st,ed:longint;
   b:array [0..maxn] of boolean;
   first,que:array [0..maxn] of longint;
   v:array [0..1,0..maxn] of longint;
   map:array [0..maxn] of atp;

procedure init;
var
   i,x,y,dis:longint;
begin
     fillchar(v,sizeof(v),100);
     readln(n,m);
     for i:=1 to m do
         begin
              readln(x,y,dis);
              inc(tot);
              map[tot].y:=y;
              map[tot].dis:=dis;
              map[tot].next:=first[x];
              first[x]:=tot;
              inc(tot);
              map[tot].y:=x;
              map[tot].dis:=dis;
              map[tot].next:=first[y];
              first[y]:=tot;
         end;
     readln(st,ed);
end;

procedure SPFA(x:longint);
var
   t:longint;
begin
     fillchar(b,sizeof(b),false);
     fillchar(que,sizeof(que),0);
     if x=0 then
        begin
             v[x,st]:=0;
             que[1]:=st;
             b[st]:=true;
        end
     else
         begin
              v[x,ed]:=0;
              que[1]:=ed;
              b[ed]:=true;
         end;
     head:=0;
     tail:=1;
     while head<tail do
           begin
                inc(head);
                t:=first[que[head]];
                while t<>0 do
                      begin
                           if v[x,que[head]]+map[t].dis<v[x,map[t].y] then
                              begin
                                   v[x,map[t].y]:=v[x,que[head]]+map[t].dis;
                                   if not b[map[t].y] then
                                      begin
                                           inc(tail);
                                           que[tail]:=map[t].y;
                                           b[map[t].y]:=true;
                                      end;
                              end;
                           t:=map[t].next;
                      end;
                b[que[head]]:=false;
           end;
end;

function max(x,y:longint):longint;
begin
     if x>y then exit(x) else exit(y);
end;

procedure outit;
var
   min,i:longint;
begin
     min:=maxlongint;
     for i:=1 to n do if max(v[0,i],v[1,i])<min then min:=max(v[0,i],v[1,i]);
     if min=v[0,0] then writeln('Peace!') else writeln(min);
end;

begin
     assign(input,'P1356.in'); reset(input);
     assign(output,'P1356.out'); rewrite(output);
     
     init;
     SPFA(0);
     SPFA(1);
     outit;
     
     close(input); close(output);
end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值