POJ 3259 wormholes
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 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 between 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-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" (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
The main idea: a person travels in the universe, there are two ways to walk. The first is the common universe, which is bidirectional and time-consuming. The second is a wormhole, one-way, and can turn the clock back. Now ask if there is a way in the universe that allows the male to return to the time before departure through the wormhole. Problem-solving ideas: When reading the normal path, remember two-way. Read into the wormhole path remember is one-way, and the weight is negative. The figure is finished, is the BF judge whether there is a negative ring. A negative ring can go back to the past, or not.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intINF =0x3f3f3f3f;Const intM =6000;Const intN = -;typedef Long LongllintN, M, W, S;structEdge {intFrom, to, Dist; }; vector<Edge>EdgesintVis[n], D[n], rec[n];intL[n];voidInit () { for(inti =0; I <= N; i++) Vis[i] =0; Edges.clear (); }voidAddedge (intFromintTo,intDist) {Edges.push_back (Edge) {from, To, Dist}); }voidInput () {scanf(" %d%d%d", &n, &m, &w);intA, B, C; 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); }}intBF () { for(inti =0; I < n; i++) D[i] = INF; d[0] =0;intA, B; for(inti =0; I < n-1; i++) {//Iteration n-1 times for(intj =0; J < Edges.size (); J + +) {//Check each sideA = Edges[j].from, B = edges[j].to;if(D[a] < INF) {D[b] = min (D[a] + edges[j].dist, d[b]);//Slack} } } for(inti =0; I < edges.size (); i++) {//If the n-1 update, you can also update, there is a negative ringA = Edges[i].from, B = edges[i].to;if(D[b] > D[a] + edges[i].dist) {return 1; } }return 0;}intMain () {intTscanf("%d", &t); while(t--) {intA, B; Init (); Input ();if(BF ())printf("yes\n");Else printf("no\n"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3259 wormholes (Bellman-ford)