poj 3259

Wormholes

Time Limit: 2000MS
Memory Limit: 65536K

Total Submissions: 11918
Accepted: 4178

Description

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.

Input

Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

Hint

For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

朴素的bellman-ford就可以过,当然也可以用spfa

顺便介绍一下bellman-ford

首先指出,图的任意一条最短路径既不能包含负权回路,也不会包含正权回路,因此它最多包含|v|-1条边。

  其次,从源点s可达的所有顶点如果 存在最短路径,则这些最短路径构成一个以s为根的最短路径树。Bellman-Ford算法的迭代松弛操作,实际上就是按顶点距离s的层次,逐层生成这棵最短路径树的过程。

  在对每条边进行1遍松弛的时候,生成了从s出发,层次至多为1的那些树枝。也就是说,找到了与s至多有1条边相联的那些顶点的最短路径;对每条边进行第2遍松弛的时候,生成了第2层次的树枝,就是说找到了经过2条边相连的那些顶点的最短路径……。因为最短路径最多只包含|v|-1 条边,所以,只需要循环|v|-1 次。

  每实施一次松弛操作,最短路径树上就会有一层顶点达到其最短距离,此后这层顶点的最短距离值就会一直保持不变,不再受后续松弛操作的影响。(但是,每次还要判断松弛,这里浪费了大量的时间,怎么优化?单纯的优化是否可行?)

  如果没有负权回路,由于最短路径树的高度最多只能是|v|-1,所以最多经过|v|-1遍松弛操作后,所有从s可达的顶点必将求出最短距离。如果 d[v]仍保持 +∞,则表明从s到v不可达。

  如果有负权回路,那么第 |v|-1 遍松弛操作仍然会成功,这时,负权回路上的顶点不会收敛。

如果使用spfa,判断一下是否有点入队n-1次就可以了(最坏情况)

View Code
type
ed
=record
a,b,t:longint;
end;
var
e:
array[1..6000]of ed;
dis:
array[1..500]of longint;
sum,i,j,jj,n,m,f,w:longint;
fff:boolean;
function bellman_ford:boolean;
var
i,j :integer;
begin
for i:=1 to n do
for j:=1 to sum do
with e[j] do
if dis[a]+t<dis[b] then
dis[b]:
=dis[a]+t;
for i:=1 to sum do
with e[i] do
if dis[a]+t<dis[b] then exit(false);
exit(true)
end;
begin
assign(input,
'input.in');
assign(output,
'output.out');
reset(input);
rewrite(output);
readln(f);
for jj:=1 to f do
begin
sum:
=0;
fff:
=false;
readln(n,m,w);
for i:=1 to m do
begin
inc(sum);
readln(e[sum].a,e[sum].b,e[sum].t);
inc(sum);
e[sum].a:
=e[sum-1].b;
e[sum].b:
=e[sum-1].a;
e[sum].t:
=e[sum-1].t;
end;
for i:=1 to w do
begin
inc(sum);
readln(e[sum].a,e[sum].b,e[sum].t);
e[sum].t:
=0-e[sum].t;
end;
for i:=1 to n do
dis[i]:
=0;
if not bellman_ford then
writeln(
'YES')
else
writeln(
'NO');
end;
close(input);
close(output);
end.
posted on 2011-02-27 20:44  leve  阅读(138)  评论(0编辑  收藏  举报