Wormholes
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 2
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.
思路:用bellman-ford 判断有没有负权回路,如果有他就能看到自己。
1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <cstring> 5 #include<cstdio> 6 #define inf 0x3f3f3f3f 7 using namespace std; 8 int n, m, p; 9 int d[505]; 10 int k = 1; 11 struct node 12 { 13 int u, v, w; 14 }e[5500]; 15 bool ballman_ford(int s) 16 { 17 int i, j; 18 for (i = 1; i <= n; i++) d[i] = inf; 19 d[s] = 0; 20 for (i = 1; i <= n - 1; i++) 21 { 22 bool up = 0; 23 for (j = 1; j <=2*m+p; j++) 24 { 25 int u = e[j].u; 26 int v = e[j].v; 27 int w = e[j].w; 28 if (d[v] > d[u] + w) 29 { 30 d[v] = d[u] + w; 31 up = 1; 32 } 33 } 34 if (up == 0) break; 35 } 36 for (j = 1; j <= 2 * m + p; j++) 37 { 38 int u = e[j].u; 39 int v = e[j].v; 40 int w = e[j].w; 41 if (d[v] > d[u] + w) 42 { 43 d[v] = d[u] + w; 44 return 1; 45 } 46 } 47 return 0; 48 } 49 int main() 50 { 51 int t; 52 cin >> t; 53 while (t--) 54 { 55 cin >> n >> m >> p; 56 int i; 57 for (i = 1; i <=2*m; i++)//双向的路 58 { 59 int x, y, z; 60 cin >> x >> y >> z; 61 e[i].u = x; 62 e[i].v = y; 63 e[i].w = z; 64 i++; 65 e[i].u = y; 66 e[i].v = x; 67 e[i].w = z; 68 } 69 for (i = 2*m+1; i <=2*m+p; i++) 70 { 71 int z; 72 cin >> e[i].u >> e[i].v >>z; 73 e[i].w = -1 * z;//虫洞权值为负 74 } 75 bool f=ballman_ford(1); 76 if (f) cout << "YES" << endl; 77 else cout << "NO" << endl; 78 } 79 return 0; 80 }