wormholesTime limit:2000msMemory limit:65536kTotal submissions:32369accepted:11762Description
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 the FJ find out whether this is possible or not, he'll supply you with complete maps to F (1≤f≤5) of the 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. 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 Betwe En S and E that requires T seconds 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 paths from S To E this 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" (does 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 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
The main topic: the farm has n points, m two-way side, each edge information is-from S to E spent time is T, the same time from T to S spent
Also for T. Now that the farm has a W wormhole, each wormhole means that the time spent from S to E is-t (the time is back, but the wormhole is a one-way side).
Q: Starting at a point in n points, can you turn back the clock.
Idea: Build a diagram, add m two-way edge, and add W from S to e weights for-t negative right side, find the longest path, see whether negative right
Circuit, if there is a negative power loop, it can make time to turn back, otherwise you can not turn back time.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 550;const int MAXM = 5500;const int INF = 0xffffff0;struct edgenode{int to; int W; int next;} Edges[maxm];int head[maxn],dist[maxn];bool bellmanford (int n,int M) {dist[1] = 0; for (int i = 1, i < n; ++i) {for (int j = 1; j <= N; ++j) {if (dist[j] = = INF) Continue for (int k = head[j]; K! =-1; k = edges[k].next) {if (EDGES[K].W! = INF && Dist[edges[k] . to] > Dist[j] + edges[k].w) dist[edges[k].to] = Dist[j] + edges[k].w; }}} for (int j = 1; j <= N; ++j) {if (dist[j] = = INF) continue; for (int k = head[j]; K! =-1; k = edges[k].next) {if (EDGES[K].W! = INF && dist[edges[k].to] ; DIST[J] + EDGES[K].W) return true; }} RetuRN false;} int main () {int f,n,m,w,s,e,t; scanf ("%d", &f); while (f--) {memset (dist,inf,sizeof (Dist)); memset (head,-1,sizeof (Head)); memset (edges,0,sizeof (Edges)); scanf ("%d%d%d", &n,&m,&w); int id = 0; for (int i = 0; i < M; ++i) {scanf ("%d%d%d", &s,&e,&t); edges[id].to = E; EDGES[ID].W = T; Edges[id].next = Head[s]; Head[s] = id++; edges[id].to = S; EDGES[ID].W = T; Edges[id].next = Head[e]; Head[e] = id++; } for (int i = 0; i < W; ++i) {scanf ("%d%d%d", &s,&e,&t); edges[id].to = E; EDGES[ID].W =-T; Edges[id].next = Head[s]; Head[s] = id++; } if (Bellmanford (n,m)) printf ("yes\n"); else printf ("no\n"); } return 0;}
POJ3259 wormholes "Bellmanford"