标签:des style blog http io ar color os sp
1.Link:
http://poj.org/problem?id=3259
2.Content:
Wormholes
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32078 Accepted: 11651 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: A single integer, F. F farm descriptions follow.
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
Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).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 8Sample Output
NO YESHint
For farm 1, FJ cannot travel back in time.
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
3.Method:
Bellman算法的模板题目,主要用来找出负环是否存在,与dis的初始值没有什么关系。
4.Code:
1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 //const int Max_d = 0; 7 8 struct Road 9 { 10 int s; 11 int e; 12 int t; 13 }; 14 15 int main() 16 { 17 //freopen("D://input.txt","r",stdin); 18 19 int i,j; 20 21 int ff; 22 cin >> ff; 23 24 int n,m,w; 25 while(ff--) 26 { 27 cin >> n >> m >> w; 28 29 Road *arr_road = new Road[m * 2 + w]; 30 31 int s,e,t; 32 for(i = 0; i < m; ++i) 33 { 34 cin >> s >> e >> t; 35 36 arr_road[i * 2].s = s; 37 arr_road[i * 2].e = e; 38 arr_road[i * 2].t = t; 39 40 arr_road[i * 2 + 1].s = e; 41 arr_road[i * 2 + 1].e = s; 42 arr_road[i * 2 + 1].t = t; 43 } 44 for(i = 0; i < w; ++i) 45 { 46 cin >> s >> e >> t; 47 48 arr_road[m * 2 + i].s = s; 49 arr_road[m * 2 + i].e = e; 50 arr_road[m * 2 + i].t = -t; 51 } 52 53 //for(i = 0; i < m * 2 + w; ++i) 54 //{ 55 // cout << i << " " << arr_road[i].s << " " << arr_road[i].e << " " << arr_road[i].t << endl; 56 //} 57 //cout << endl; 58 59 int *arr_d = new int[n]; 60 //for(i = 0; i < n; ++i) arr_d[i] = Max_d; 61 memset(arr_d,0,sizeof(int) * n); 62 63 //Bellman-Ford 64 bool flag; 65 for(i = 0; i < n - 1; ++i) 66 { 67 flag = false; 68 for(j = 0; j < 2 * m + w; ++j) 69 { 70 if(arr_d[arr_road[j].e] > arr_d[arr_road[j].s] + arr_road[j].t) 71 { 72 arr_d[arr_road[j].e] = arr_d[arr_road[j].s] + arr_road[j].t; 73 flag = true; 74 } 75 } 76 if(!flag) break; 77 } 78 79 for(j = 0; j < 2 * m + w; ++j) 80 { 81 if(arr_d[arr_road[j].e] > arr_d[arr_road[j].s] + arr_road[j].t) break; 82 } 83 84 if(j < 2 * m + w) cout << "YES" << endl; 85 else cout << "NO" << endl; 86 87 delete [] arr_d; 88 delete [] arr_road; 89 } 90 91 return 0; 92 }
5.Reference:
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/mobileliker/p/4127259.html