首页 > 代码库 > poj3259-Wormholes(Bellman-Ford 最短路)
poj3259-Wormholes(Bellman-Ford 最短路)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 32109 | Accepted: 11660 |
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
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES
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
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. 一个是单向边,而且单向边为负值。
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 const int VM=520; 6 const int EM=5020; 7 const int INF=0x3f3f3f3f; 8 9 struct Edge{10 int u,v;11 int c;12 }edge[EM<<1];13 14 int n, m, w, cnt, dis[VM];15 16 17 void add(int u, int v, int c)18 {19 edge[cnt].u = u;20 edge[cnt].v = v;21 edge[cnt].c = c;22 cnt++;23 }24 int Bellman()25 {26 int i, j, k, flag;27 for(i=0; i<n; i++)28 dis[i] = INF;29 for(i=1; i<n; i++)30 {31 flag = 0;32 for(j=0; j<cnt; j++)33 {34 if(dis[edge[j].v] > dis[edge[j].u] + edge[j].c)35 {36 dis[edge[j].v] = dis[edge[j].u] + edge[j].c;37 flag = 1;38 }39 }40 if(!flag) break;41 }42 for(k=0; k<cnt; k++)43 if(dis[edge[k].v] > dis[edge[k].u] + edge[k].c)44 return 1;45 return 0;46 47 }48 int main()49 {50 int T, i;51 scanf("%d", &T);52 while(T--)53 {54 int x, y, z;55 cnt = 0;56 scanf("%d%d%d", &n, &m, &w);57 for(i=0; i<m; i++)58 {59 scanf("%d%d%d", &x, &y, &z);60 add(x, y, z);//加边61 add(y, x, z);62 }63 for(i=0; i<w; i++)64 {65 scanf("%d%d%d", &x, &y, &z);66 add(x, y, -z);//注意这里是-z67 }68 if(Bellman())69 printf("YES\n");70 else printf("NO\n");71 }72 return 0;73 }
poj3259-Wormholes(Bellman-Ford 最短路)