首页 > 代码库 > POJ 3259 Wormholes(Bellman-Ford)
POJ 3259 Wormholes(Bellman-Ford)
题目网址:http://poj.org/problem?id=3259
题目:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 52198 | Accepted: 19426 |
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.
我们根本不需要关心他所处的起点的具体位置,我们只需要判断是否有负权环即可,所以将所有的dist[i]都初始化为无穷大。有负权环的话就输出YES,没有的话就输出NO。很自然地就会想到Bellman-Ford算法。判断第n次循环,是否还会松弛,如果还需要就说明有负权环。 这道题需要注意的一点是:虫洞是单向边,路径是双向边。
1 #include <cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 const int inf = 111111111; 7 struct node{ 8 int v,u,w; 9 }; 10 vector<node>v; 11 int n,m,w; 12 int dist[505]; 13 node x; 14 bool relax(int j){//松弛操作 15 if(dist[v[j].u]>dist[v[j].v]+v[j].w){ 16 dist[v[j].u]=dist[v[j].v]+v[j].w; 17 return true; 18 } 19 return false; 20 } 21 bool bellman_ford(){ 22 for (int i=1; i<=n; i++) { 23 dist[i]=inf; 24 } 25 for (int i=0; i<n-1; i++) { 26 int flag=0; 27 for (int j=0; j<v.size(); j++) { 28 if(relax(j)) flag=1; 29 } 30 if(!flag) return false; 31 } 32 for (int j=0; j<v.size(); j++) {//核心 33 if(relax(j)) return true; 34 } 35 return false; 36 } 37 int main(){ 38 int t; 39 cin>>t; 40 while (t--) { 41 int ok=0; 42 v.clear(); 43 cin>>n>>m>>w; 44 for (int i=0; i<m; i++) { 45 cin>>x.v>>x.u>>x.w; 46 v.push_back(x); 47 swap(x.v, x.u); 48 v.push_back(x); 49 } 50 for (int i=0; i<w; i++) { 51 cin>>x.v>>x.u>>x.w; 52 x.w=0-x.w; 53 v.push_back(x); 54 } 55 if (bellman_ford()) printf("YES\n"); 56 else printf("NO\n"); 57 } 58 return 0; 59 }
POJ 3259 Wormholes(Bellman-Ford)