POJ 3259 Wormholes (最短路)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 34302 | Accepted: 12520 |
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of 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 an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
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. Two fields might be 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 way path from S to E that also moves the traveler back T seconds.
Output
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 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int INF = 0xfffffff; 6 const int SIZE = 5500; 7 int D[505]; 8 int N,M,W,F; 9 struct Node 10 { 11 int from,to,cost; 12 }G[SIZE]; 13 14 bool Bellman_Ford(int); 15 bool relax(int,int,int); 16 int main(void) 17 { 18 scanf("%d",&F); 19 while(F --) 20 { 21 scanf("%d%d%d",&N,&M,&W); 22 int i = 0; 23 while(i < 2 * M) 24 { 25 scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost); 26 i ++; 27 G[i] = G[i - 1]; 28 swap(G[i].from,G[i].to); 29 i ++; 30 } 31 while(i < W + M * 2) 32 { 33 scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost); 34 G[i].cost = -G[i].cost; 35 i ++; 36 } 37 bool flag = true; 38 for(int i = 1;i <= N;i ++) 39 if(!Bellman_Ford(i)) 40 { 41 puts("YES"); 42 flag = false; 43 break; 44 } 45 if(flag) 46 puts("NO"); 47 } 48 49 return 0; 50 } 51 52 bool Bellman_Ford(int s) 53 { 54 fill(D,D + 505,INF); 55 D[s] = 0; 56 bool update; 57 58 for(int i = 0;i < N - 1;i ++) 59 { 60 update = false; 61 for(int i = 0;i < M * 2 + W;i ++) 62 if(relax(G[i].from,G[i].to,G[i].cost)) 63 update = true; 64 if(!update) 65 break; 66 } 67 for(int i = 0;i < M * 2 + W;i ++) 68 if(relax(G[i].from,G[i].to,G[i].cost)) 69 return false; 70 return true; 71 } 72 73 bool relax(int from,int to,int cost) 74 { 75 if(D[to] > D[from] + cost) 76 { 77 D[to] = D[from] + cost; 78 return true; 79 } 80 return false; 81 }