wormholes
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 36425 |
|
Accepted: 13320 |
Description
While exploring he many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it's a one-way path that delivers the IT destination at a time that's before you entered the wormhole! Each of the 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 a avid time-traveling fan, he wants to does the following:start at some field, travel through some paths and worm Holes, and return to the starting field a time before his initial departure. Perhaps he'll be able to meet himself:).
To help FJ find out whether this is possible or not, he'll supply you with complete maps to F (1≤ f ≤ 5) of his farms. No paths'll take longer than seconds to travel and no wormhole can bring FJ back in time by more than-seco Nds.
Input
Line 1: A single integer,
F.
FFarm 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
Sand
EThat requires
TSeconds to traverse. The might is 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-path from
STo
EThat also moves the traveler back
TSeconds.
Output
Lines 1..
F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (does not include the quotes).
Sample Input
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES
Hint
For farm 1, FJ cannot travel back in time.
For Farm 2, FJ could travel back on time by the cycle 1->2->3->1, arriving back at he starting location 1 second Before he leaves. He could start from anywhere in the cycle to accomplish this.
Source
Usaco 2006 December Gold turn: Bellmanford template http://blog.csdn.net/niushuai666/article/details/6791765 Test instructions: Determine if there is a negative ring. Harvest: Bellmanford: Relax the n-1 wheel, use all sides every time, if still existdis[Edge [J].v] > dis[Edge[J].u] + Edge[j].W has a negative ring.
#include <cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cmath>#include<string>#include<cstring>#include<stack>#include<queue>#include<list>#include<vector>#include<map>#include<Set>using namespacestd;Const intinf=0x3f3f3f3f;Const Doubleeps=1e-Ten;Const DoublePi=acos (-1.0);Const intmaxn= the+ -;intN;structedge{intu, V, W, next;}; Edge EDGE[MAXN];intnum;intHEAD[MAXN];voidInit_edge () {num=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintVintW) {edge[num].u=u; EDGE[NUM].V=v; EDGE[NUM].W=W; Edge[num].next=Head[u]; Head[u]= num++;}intDIS[MAXN];BOOLBellmanford ()//bellmanford template { for(inti =1; I <= N; i++) Dis[i] =INF; dis[1] =0; for(inti =0; I < n; i++) { for(intj =0; J < num; J + +) { if(DIS[EDGE[J].V] > dis[edge[j].u] +edge[j].w) DIS[EDGE[J].V]= Dis[edge[j].u] +EDGE[J].W; } } //BOOL flag = 1; for(intj =0; J < num; J + +) { if(DIS[EDGE[J].V] > dis[edge[j].u] +EDGE[J].W)return 0; } return 1;}intMain () {intT; scanf ("%d", &t); while(t--) { intM, W; scanf ("%d%d%d", &n, &m, &W); intA, B, C; Init_edge (); for(inti =0; I < m; i++) {scanf ("%d%d%d", &a, &b,&c); Addedge (A, B, c); Addedge (b, A, c); } for(inti =0; i < W; i++) {scanf ("%d%d%d", &a, &b,&c); Addedge (A, B,-c); } //int flag = 0; if(!Bellmanford ()) printf ("yes\n"); Elseprintf ("no\n"); } return 0;}
POJ 3259 wormholes (Bellmanford award negative ring)