Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 37423 | Accepted: 13770 |
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
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
Source
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #define INF 11000 using namespace std; int dis[555]; struct ant{ int start, end, time; }p[5555]; int n,m,w; int bellman_ford() { int i,j,k; int con = 1,f = 1; memset(dis,INF,sizeof(dis)); while(f) { f = 0; if(con++ > n) return 1; for(i = 1; i <= m; i++) { if(dis[p[i].start]+p[i].time < dis[p[i].end]) { dis[p[i].end]=dis[p[i].start]+p[i].time; f = 1; } if(dis[p[i].end]+p[i].time < dis[p[i].start]) { f = 1; dis[p[i].start]=dis[p[i].end]+p[i].time; } } for( ; i <= m+w;i++) { if(dis[p[i].start]-p[i].time < dis[p[i].end]) { dis[p[i].end]=dis[p[i].start]-p[i].time; f = 1; } } } return 0; } int main() { int i,j,k; int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n, &m, &w); for(i = 1; i <= m+w; i++) { scanf("%d%d%d",&p[i].start, &p[i].end, &p[i].time); } if(bellman_ford()) puts("YES"); else puts("NO"); } return 0; }