Poj3259 Wormholes, poj3259wormholes
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, And
W
Lines 2 ..
M+ 1 of each farm: Three space-separated numbers (
S,
E,
T) That describe, respectively: a bidirectional path
SAnd
EThat requires
TSeconds 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
STo
EThat also moves the traveler back
TSeconds.
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.
Question: N places, M roads, and W wormhole holes. Determine whether the path can be time-backed. Replace W [I] With-w [I].
SPFA: determines whether the number of teams has exceeded N.
Note:Don't trust the data range. This is a big pitfall !! There are definitely more than a lot of test data. I opened an array of 20000.
# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <queue> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <cmath> # define INF 0x3f // a large number # define eps 1e-6using namespace std; # define maxn 20000 // increase the size of the array! # Define Maxn 20000int n, m, W; int first [maxn]; int next [maxn]; int u [Maxn]; int v [Maxn]; int w [Maxn]; int dis [maxn]; int go [maxn]; int vist [maxn]; int conut [maxn]; int ttt; queue <int> Q; int spfa (int x) {vist [x] = 1; memset (dis, INF, sizeof (dis); dis [x] = 0; Q. push (x); conut [x] ++; while (! Q. empty () {int t = Q. front (); Q. pop (); vist [t] = 0; conut [t] ++; for (int I = first [t]; I! =-1; I = next [I]) {if (dis [t] + w [I] <dis [v [I]) {dis [v [I] = dis [t] + w [I]; if (! Vist [v [I]) {if (conut [t]> = n) // if a node has been in the queue for more than n times, it can be determined that there is a negative ring .... Return 1; Q. push (v [I]); ++ conut [v [I] ;}}}return 0 ;}int main () {int T; cin >> T; while (T --) {memset (vist, 0, sizeof (vist); memset (conut, 0, sizeof (conut); while (! Q. empty () Q. pop (); // cout <" @" <endl; scanf ("% d", & n, & m, & W ); for (int I = 1; I <= n; I ++) first [I] =-1; for (int I = 1; I <= m; I ++) {scanf ("% d", & u [I], & v [I], & w [I]); u [I + m] = v [I]; v [I + m] = u [I]; w [I + m] = w [I];} for (int I = m * 2 + 1; I <= 2 * m + W; I ++) {scanf ("% d ", & u [I], & v [I], & w [I]); w [I] =-w [I] ;}for (int I = 1; I <= 2 * m + W; I ++) {next [I] = first [u [I]; first [u [I] = I ;} int qq = spfa (1); if (qq) puts ("YES"); else puts ("NO");} return 0 ;}
Poj3259 bellman Algorithm
Check out my code.
# Include <iostream>
Using namespace std;
Typedef struct Node
{
Int u, v, t;
} Node;
Node e [2, 25000];
Int n, m, w, en;
Bool bellmanford ()
{
Bool flag;
Int dis [1001];
For (int I = 0; I <n-1; I ++)
{
Flag = false;
For (int j = 0; j <en; j ++)
If (dis [e [j]. v]> dis [e [j]. u] + e [j]. t)
{
Dis [e [j]. v] = dis [e [j]. u] + e [j]. t;
Flag = true;
}
If (! Flag)
Break;
}
For (int I = 0; I <en; I ++)
If (dis [e [I]. v]> dis [e [I]. u] + e [I]. t)
Return true;
Return false;
}
Int main ()
{
Int Case, u, v, t;
// Freopen ("in.txt", "r", stdin );
// Freopen ("out.txt", "w", stdout );
Scanf ("% d", & Case );
While (Case --)
{
Scanf ("% d", & n, & m, & w );
En = 0;
For (int I = 1; I <= m; I ++)
{
Scanf ("% d", & u, & v, & t );
E [en]. u = u;
E [en]. v = v;
E [en ++]. t = t;
E [en]. u = v;
E [en]. v = u;
E [en ++]. t = t;
}
For (int I = 1; I <= w; I ++)
{
Scanf ("% d", & u, & v, & t );
E [en]. u = u;
E [en]. v = v;
E [en ++]. t =-t;
}
If (bellmanford ())
Puts ("YES ");
Else
Puts ("NO ");
}
Return 0;
}... Remaining full text>