Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 36836 | Accepted: 13495 |
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.
Source
1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 int dis[550], vis[550], used[550]; //used[]数组判负环; 8 int n, m, t; 9 struct Edge 10 { 11 int from, to, w, next; 12 } edge[5050]; 13 int head[550], cnt; 14 void Add(int a, int b, int w) 15 { 16 Edge E = {a, b, w, head[a]}; 17 edge[cnt] = E; 18 head[a] = cnt++; 19 } 20 bool Spfa(int src) 21 { 22 queue<int> q; 23 for(int i = 1; i <= n; i++) 24 dis[i] = INF; 25 dis[src] = 0; vis[src] = 1; 26 q.push(src); 27 while(!q.empty()) 28 { 29 // printf("1 "); 30 int u = q.front(); 31 q.pop(); 32 vis[u] = 0; 33 for(int i = head[u]; i != -1; i = edge[i].next) 34 { 35 int v = edge[i].to; 36 if(dis[v] > dis[u] + edge[i].w) 37 { 38 dis[v] = dis[u] + edge[i].w; 39 if(!vis[v]) 40 { 41 vis[v] = 1; 42 q.push(v); 43 used[v]++; 44 } 45 } 46 if(used[v] > n) //存在负环; 47 return true; 48 } 49 } 50 return false; 51 } 52 int main() 53 { 54 int nt; 55 scanf("%d", &nt); 56 while(nt--) 57 { 58 scanf("%d %d %d", &n, &m, &t); 59 int u, v, w; cnt = 0; 60 memset(vis, 0, sizeof(vis)); 61 memset(used, 0 , sizeof(used)); 62 memset(head, -1, sizeof(head)); 63 for(int i = 0; i < m; i++) 64 { 65 scanf("%d %d %d", &u, &v, &w); 66 Add(u, v, w); 67 Add(v, u, w); 68 } 69 for(int i = 1; i <= t; i++) 70 { 71 scanf("%d %d %d", &u, &v, &w); 72 Add(u, v, -w); 73 } 74 if(Spfa(1)) 75 printf("YES "); 76 else 77 printf("NO "); 78 } 79 return 0; 80 }