标签:
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
1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn=550; 8 const int maxm=2555; 9 const int inf=0x3f3f3f3f; 10 int G[maxn][maxn]; 11 bool vis[maxn]; 12 int used[maxn]; 13 int d[maxn]; 14 int cnt; 15 int m,n1,n2; 16 17 bool spfa(int s) 18 { 19 queue<int>q; 20 memset(vis,0,sizeof(vis)); 21 memset(d,inf,sizeof(d)); 22 memset(used,0,sizeof(used)); 23 d[s]=0; 24 q.push(s); 25 vis[s]=1; 26 used[s]++; 27 while(!q.empty()) 28 { 29 int u=q.front(); 30 q.pop(); 31 vis[u]=0; 32 for(int i=1;i<=m;i++) 33 { 34 if(d[i]>d[u]+G[u][i]) 35 { 36 d[i]=d[u]+G[u][i]; 37 if(!vis[i]) 38 { 39 if(used[i]>=maxn) 40 return true; 41 q.push(i); 42 vis[i]=1; 43 used[i]++; 44 } 45 } 46 } 47 } 48 return false; 49 } 50 51 int main() 52 { 53 int t,u,v,w; 54 scanf("%d",&t); 55 while(t--) 56 { 57 memset(G,inf,sizeof(G)); 58 scanf("%d%d%d",&m,&n1,&n2); 59 for(int i=0;i<n1;i++) 60 { 61 scanf("%d%d%d",&u,&v,&w); 62 G[u][v]=G[v][u]=min(G[u][v],w); 63 } 64 for(int i=0;i<n2;i++) 65 { 66 scanf("%d%d%d",&u,&v,&w); 67 G[u][v]=min(G[u][v],-w); 68 } 69 int ans=spfa(1); 70 if(ans) 71 printf("YES\n"); 72 else 73 printf("NO\n"); 74 } 75 return 0; 76 }
标签:
原文地址:http://www.cnblogs.com/homura/p/4741624.html