poj 3259--Wormholes
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 Tseconds 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 <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 #define INF 0x7f7f7f7 6 using namespace std; 7 int dis[510],vis[510],num[510]; 8 int head[1000]; 9 int n,cnt; 10 struct edge 11 { 12 int v,w,next; 13 }e[10000]; 14 void addedge(int a,int b,int c) 15 { 16 e[cnt].v=b; 17 e[cnt].w=c; 18 e[cnt].next=head[a]; 19 head[a]=cnt++; 20 } 21 int spfa() 22 { 23 int i; 24 for(i=0;i<=n;i++) 25 { 26 dis[i]=INF; 27 vis[i]=num[i]=0; 28 } 29 dis[1]=0; 30 vis[1]=1; 31 num[1]++; 32 deque<int>q; 33 q.push_back(1);//slf优化 34 while(!q.empty()) 35 { 36 int x=q.front(); 37 q.pop_front(); 38 vis[x]=0; 39 for(i=head[x];i!=-1;i=e[i].next) 40 { 41 if(dis[e[i].v]>dis[x]+e[i].w) 42 { 43 dis[e[i].v]=dis[x]+e[i].w; 44 if(!vis[e[i].v]) 45 { 46 vis[e[i].v]=1; 47 if(++num[e[i].v]>n) 48 return 0; 49 if(!q.empty()) 50 { 51 if(dis[e[i].v]>dis[q.front()]) 52 q.push_back(e[i].v); 53 else 54 q.push_front(e[i].v); 55 } 56 q.push_back(e[i].v); 57 } 58 } 59 } 60 } 61 return 1; 62 } 63 64 int main() 65 { 66 int t,m,w; 67 int a,b,c; 68 scanf("%d",&t); 69 while(t--) 70 { 71 cnt=0; 72 scanf("%d%d%d",&n,&m,&w); 73 memset(head,-1,sizeof(head)); 74 while(m--) 75 { 76 scanf("%d%d%d",&a,&b,&c); 77 addedge(a,b,c); 78 addedge(b,a,c); 79 } 80 while(w--) 81 { 82 scanf("%d%d%d",&a,&b,&c); 83 addedge(a,b,-c); 84 } 85 if(!spfa()) 86 printf("YES\n"); 87 else 88 printf("NO\n"); 89 } 90 return 0; 91 }
1 #include <stdio.h> 2 struct edge 3 { 4 int u, v, t; 5 } edges[5220]; 6 int el, n, d[501]; 7 void bellman() 8 { 9 int i, j, flag; 10 for (i = 1; i < n; i++) 11 { 12 flag = 0; 13 for (j = 0; j < el; j++) 14 if (d[edges[j].v] > d[edges[j].u] + edges[j].t) 15 { 16 d[edges[j].v] = d[edges[j].u] + edges[j].t; 17 flag = 1; 18 } 19 if (!flag) 20 { 21 puts("NO"); 22 return; 23 } 24 } 25 for (i = 0; i < el; i++) 26 if (d[edges[i].v] > d[edges[i].u] + edges[i].t) 27 { 28 puts("YES"); 29 return; 30 } 31 puts("NO"); 32 } 33 int main(void) 34 { 35 int f, m, w, s, e, t; 36 int i, j; 37 scanf("%d", &f); 38 while (f--) 39 { 40 el = 0; 41 scanf("%d %d %d", &n, &m, &w); 42 for (i = 0; i < m; i++) 43 { 44 scanf("%d %d %d", &s, &e, &t); 45 edges[el].u = s; 46 edges[el].v = e; 47 edges[el++].t = t; 48 edges[el].u = e; 49 edges[el].v = s; 50 edges[el++].t = t; 51 } 52 for (i = 0; i < w; i++) 53 { 54 scanf("%d %d %d", &s, &e, &t); 55 edges[el].u = s; 56 edges[el].v = e; 57 edges[el++].t = -t; 58 } 59 bellman(); 60 } 61 return 0; 62 }