http://poj.org/problem?id=3259
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.
SPFA to determine whether there is a negative right, if a point into the queue to the total number of points indicates a negative right
Dist[i] Array records the shortest path of source point to I, unlike Dijsktar Dist[i] multiple updates
Use[i] Record the number of times I enter the queue, that is, Dist[i] is updated;
Vis[i] Mark I point whether to enter the queue
#include <stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<vector>#include<queue>#defineINF 0XFFFFFF#defineN 520using namespacestd;structnode{inte, W;}; Vector<node>G[n];intN, Use[n], dist[n];BOOLVis[n];voidInit () {inti; memset (Vis,false,sizeof(VIS)); memset (use,0,sizeof(use)); for(i =0; I <= N; i++) {g[i].clear (); Dist[i]=INF; }}intSPFA (ints) {Queue<node>Q; Node now, next; intI, Len; NOW.E=s; NOW.W=0; Dist[s]=0; Q.push (now); Vis[s]=true; USE[NOW.E]++; while(!Q.empty ()) { Now=Q.front (); Q.pop (); VIS[NOW.E]=false; Len=g[now.e].size (); for(i =0; i < Len; i++) {Next=G[now.e][i]; if(DIST[NEXT.E] > DIST[NOW.E] +next.w) {DIST[NEXT.E]= dist[now.e] +NEXT.W; USE[NEXT.E]++; if(USE[NEXT.E] >=N)return 1; if(!VIS[NEXT.E]) {VIS[NEXT.E]=true; Q.push (next); } } } } return 0;}intMain () {intT, M, W, S, E, t, I; Node p; scanf ("%d", &T); while(t--) {scanf ("%d%d%d", &n, &m, &W); Init (); for(i =1; I <= m; i++) {scanf ("%d%d%d", &s, &e, &t); P.W=T; P.E=s; G[e].push_back (P); P.E=e; G[s].push_back (P); } for(i =1; I <= W; i++) {scanf ("%d%d%d", &s, &e, &t); P.W= -T; P.E=e; G[s].push_back (P); } if(SPFA (1)) printf ("yes\n"); Elseprintf ("no\n"); } return 0;}
POJ wormholes (SPFA)