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

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..NM (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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值