http://poj.org/problem?id=3259
Description
While exploring he many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it's a one-way path that delivers the IT destination at a time that's before you entered the wormhole! Each of the 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 a avid time-traveling fan, he wants to does the following:start at some field, travel through some paths and worm Holes, and return to the starting field a time before his initial departure. Perhaps he'll be able to meet himself:).
To help FJ find out whether this is possible or not, he'll supply you with complete maps to F (1≤ f ≤ 5) of his farms. No paths'll take longer than seconds to travel and no wormhole can bring FJ back in time by more than-seco Nds.
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 between
Sand
EThat requires
TSeconds to traverse. The might is 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-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" (does 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
/* Topic: Wormhole problem, now there are n points, M-bar, representing the path now can go, such as from A to B and from B to a need to spend C time, now on the ground has a w wormhole, the meaning of the wormhole is that you spend from A to B time is-C (time back, and wormhole is unidirectional), Now ask you to go from some point, can go back to the past is to judge whether there is negative ring */#include <cstdio> #include <cstring> #include <queue> #define MAXN 550#define maxm 10000#define INF 0x3f3f3f3fusing namespace std;struct edge{int u, V, w;int next;//next structure variable subscript}EDGE[MAXM]; The int head[maxn];//subscript is the starting point U, and the value is the corresponding struct subscript int vis[maxn];//Determines whether to join the queue int used[maxn];int num;int n;int low[maxn];//Save Shortest path void add_ Edge (int u, int v, int w)//Add edge {edge e={u, V, W, head[u]};//initialize struct edge[num]=e;//direct assignment head[u]=num++;} BOOL SPFA (int s) {int i, j;queue<int> q;memset (Low, INF, sizeof), memset (Vis, 0, sizeof (VIS)), memset (used,0, sizeof (used)); Vis[s] = 1;low[s]=0; Q.push (s); Used[s]++;while (! Q.empty ()) {int U=q.front (); Q.pop (); vis[u]=0;//out of the queue, not in the queue becomes 0 for (j = head[u]; j =-1; j = edge[j].next) {int v = edge[j].v;if (Low[v] > Low[u] + edg E[J].W) {Low[v] = Low[u] + edge[j].w;if (!vis[v]) {vis[v]=1; Q.push (v); used[v]++; if (used[v]>n) return 0; }}}}return 1;} int main () {int u, V, w;int t;int M, w;scanf ("%d", &t), while (t--) {scanf ("%d%d%d", &n, &m, &w), memset (head, -1, sizeof (head)); Num=0;while (m--) {scanf ("%d%d%d", &u, &v, &w); Add_edge (U, V, W); Add_edge (V, U, W);//Non-}while (w--) {scanf ("%d%d%d", &u, &v, &w); Add_edge (U, V,-W);} if (! SPFA (1)) printf ("yes\n"), Else printf ("no\n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
wormholes POJ 3259 "SPFA"