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。
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.
相关文章推荐
- POJ ~ 3259 ~ Wormholes (BellmanFord和SPFA模板)
- POJ 3259-Wormholes (Bellman-Ford&&SPFA) (模板题)
- poj 3259 Wormholes(spfa) (spfa模板)
- Wormholes( POJ 3259)(Bellman-Ford+SPFA)(判断是否有负权环)(最短路模板)
- poj2449 Remmarguts' Date --- k短路模板(SPFA+A*)
- poj&nbsp;3259spfa()判断是否存在负环
- poj 3259 Wormholes【spfa判断负环】
- POJ 3259:Wormholes 【SPFA】
- poj-1125-Stockbroker Grapevine spfa模板
- poj 3259 spfa/bellman判断负圈(虫洞)
- poj 3259 Wormholes (spfa求最短路)
- F - Wormholes POJ - 3259--SPFA 算法判断负环是否存在
- poj 1511 Invitation Cards 最短路SPFA(模板题)
- poj 3259 Wormholes【spfa判负环】
- POJ 3259 Wormholes(SPFA+邻接表)
- spfa poj 3259
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
- poj 3259 Wormholes(spfa 判环)
- POJ - 3259 Wormholes(SPFA最短路判断负环)
- POJ 3259 SPFA判断负权回路