Link: http://poj.org/problem? Id = 3259 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 22010 # Problem/B
Wormholes
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:25079 |
|
Accepted:8946 |
Description While processing his own 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 ComprisesN(1 ≤N≤ 500) fields conveniently numbered 1 ..N,M(1 ≤M≤ 2500) paths, andW(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 mapsF(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.FFarm descriptions follow. Line 1 of each farm: three space-separated integers respectively:N,M, AndW Lines 2 ..M+ 1 of each farm: three space-separated numbers (S,E,T) That describe, respectively: a bidirectional pathSAndEThat requiresTSeconds to traverse. Two fields might be connected By more than one path. LinesM+ 2 ..M+W+ 1 of each farm: three space-separated numbers (S,E,T) That describe, respectively: a one way path fromSToEThat also moves the traveler backTSeconds.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 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 1, FJ cannot travel back in time. For Farm 2, FJ cocould travel back in time by the cycle 1-> 2-> 3-> 1, arriving back at his starting location 1 second before he leaves. he cocould start from anywhere on the cycle to accomplish this.Source Usaco 2006 December gold |
Question:
Farmer FJ has n fields [1 .. n] (1 <= n <= 500)
There are m paths between fields 【Bidirectional] (1 <= m <= 2500)
At the same time, there are W holes that can be returned to a previous time point 【Unidirectional] (1 <= W <= 200)
Q: Can FJ meet his previous colleagues in the field?
Algorithm: bellman_ford determines whether a negative ring exists
Ideas:
Field
BidirectionalPath edge adding with the weight
Zheng
Between holes
UnidirectionalPath edge adding with the weight
Negative[Back to the past]
Determine whether a directed graph has a negative ring
Because if a negative ring exists, the time will be continuously reduced,
Then FJ will be able to go back to the distance before, and will certainly be able to meet his own PS: The first time I made this shoes, if it is really incomprehensible, just draw a graph based on the above examples and ideas. It's only three points. After two years, I have never encountered such a classic entry-level question. I really don't know what I 've done with orzcode:
3259 |
Accepted |
180 K |
63 MS |
C ++ |
1707b |
/*************************************** * *************************** Accepted180 kb47 MSC ++ 2509 B: farmer FJ has n fields... n] (1 <= n <= 500) m paths between the fields [bidirectional] (1 <= m <= 2500) W holes at the same time, you can go back to the previous time point [one-way] (1 <= W <= 200) and ask: Can FJ meet its own algorithm in the field: bellman_ford, to determine whether there is a negative cycle: bidirectional path edge adding between fields. The weight is the unidirectional path edge adding between holes, if the weight value is negative, you can return to the previous step to determine whether a digraph has a negative ring. Because if a negative ring exists, the time will be continuously reduced, and FJ will be able to go back to the previous step, you must have met your own ********************************* * *********************************/# include <stdio. h> # include <string. h >#include <algorithm> # include <iostream> using namespace STD; const int maxn = 510; const int maxw = 2500*2 + 200 + 10; const int INF = 10000; int d [maxn]; int n, m; struct edge {int U, V; int t;} edge [maxw]; bool bellman_ford () {for (INT I = 1; I <= N; I ++) d [I] = inf; // The initialization time from the start point to I is d [1] = 0; // start point is 0 for (INT I = 1; I <n; I ++) {bool flag = true; // determine whether this round can be relaxed for (Int J = 0; j <m; j ++) {int u = edge [J]. u; int v = edge [J]. v; int T = edge [J]. t; If (d [v]> d [u] + T) // relaxation operation {d [v] = d [u] + T; flag = false ;}} if (FLAG) return false; // if the current wheel cannot be relaxed, directly determine that there is no negative Ring} For (INT I = 0; I <m; I ++) {If (d [edge [I]. v]> d [edge [I]. u] + edge [I]. t) return true; // if it can still be relaxed, there will be a negative Ring} return false;} int main () {int t; int M, W; scanf ("% d ", & T); While (t --) {scanf ("% d", & N, & M, & W); M = 0; int U, V, t; for (INT I = 1; I <= m; I ++) // the main road between fields, plus bilateral {scanf ("% d ", & U, & V, & T); edge [M]. U = u; edge [M]. V = V; edge [M ++]. T = T; edge [M]. U = V; edge [M]. V = u; edge [M ++]. T = T ;}for (INT I = 1; I <= W; I ++) // holes, add a single side {scanf ("% d ", & U, & V, & T); edge [M]. U = u; edge [M]. V = V; edge [M ++]. T =-T;} If (bellman_ford () printf ("Yes \ n"); // there is a negative ring else printf ("NO \ n ");}}