您的位置:首页 > 其它

Poj 3259 Wormholes判断负权回路(spfa模板题)

2016-11-13 20:30 197 查看
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a
time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial
departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to
travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
---------------------------------------------------------------------分割线-------------------------------------------------------------------------------------
    朴素的SPFA,判断有无负权回路即可。注意普通道路是无向的。完美的打一遍模板,AK。

var
t:boolean;
f,i,n:longint;
way:array[1..500,1..500] of longint;

function min(a,b:longint):longint;
begin
if a<=b then exit(a) else exit(b);
end;

procedure Readin;
var
i,j,si,ei,ti,m,w:longint;
begin
read(n,m,w);
for i:=1 to n do for j:=1 to n do way[i,j]:=maxlongint;
for i:=1 to m+w do begin
read(si,ei,ti);if i>m then ti:=-ti;
way[si,ei]:=min(ti,way[si,ei]);
if i<=m then way[ei,si]:=way[si,ei];
end;
end;

procedure SPFA;
var
time,dis:array [1..500] of longint;
used:array[1..500] of boolean;
que:array[1..1000000] of longint;
head,tail,i,x:longint;
begin
t:=false;
dis[1]:=0;for i:=2 to n do dis[i]:=maxlongint;
fillchar(used,sizeof(used),false);
fillchar(time,sizeof(time),0);
head:=1;tail:=1;que[1]:=1;time[1]:=1;
while head<=tail do begin
x:=que[head];
for i:=1 to n do
if (way[x,i]<>maxlongint) and (dis[i]>dis[x]+way[x,i]) then begin
dis[i]:=dis[x]+way[x,i];
if not used[i] then begin
if time[i]+1>n then begin
t:=true;
break;
end;
inc(time[i]);
inc(tail);
que[tail]:=i;
end;
end;
used[head]:=false;
inc(head);
end;
end;

procedure Print;
begin
if t then writeln('YES')
else writeln('NO');
end;

begin
read(f);
for i:=1 to f do begin
Readin;
SPFA;
Print;
end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: