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.
Test Instructions: There are n fields, M-Paths, W-time tunnels (it is amazing that it allows FJ to go straight from field A to field B and back to C seconds), now tell you the path and Time tunnel, and find out if FJ can see another one that hasn't started yet. (You can see yourself if you can use the time tunnel to return to the starting point and arrive at a time tunnel that is less time-traveling than the time tunnel)
#include <stdio.h>#include<queue>#include<algorithm>using namespacestd;Const intn=510;Const intinf=0x3f3f3f3f;intG[n][n], Dist[n], cou[n], vis[n], N;///cou Array saves the number of times the point is traversedvoidInit () {intI, J; for(i =1; I <= N; i++) {Dist[i]=INF; Cou[i]= Vis[i] =0; for(j =1; J <= N; J + +) G[i][j]=INF; G[i][i]=0; }}intSPFA () {intI, V; Queue<int>p; Q.push (1); dist[1] =0; cou[1]++; while(!Q.empty ()) {v=Q.front (); Q.pop (); for(i =1; I <= N; i++) { if(Dist[i] > g[v][i]+Dist[v]) {Dist[i]= g[v][i]+Dist[v]; if(!Vis[i]) {Q.push (i); Vis[i]=1; Cou[i]++; if(Cou[i] >= n && dist[i] <0) return 1;///If there is a loop and the time to get to the time tunnel is smaller than the time tunnel shuttle}}} Vis[v]=0; } return 0;}intMain () {intT, A, B, C, M, W, ans; scanf ("%d", &T); while(t--) {scanf ("%d%d%d", &n, &m, &W); Init (); while(m--) {scanf ("%d%d%d", &a, &b, &c); G[A][B]=min (g[a][b], c); G[b][a]=G[a][b]; } while(w--) {scanf ("%d%d%d", &a, &b, &c); G[A][B]=-C;///because the time tunnel can go back to c seconds ago, the direct assignment is C.} ans=SPFA (); if(ANS) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
HDU 3259 wormholes