Wormholes
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 32111 |
|
Accepted: 11662 |
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
Hint
For farm 1, FJ cannot travel back in time.
For Farm 2, FJ could travel back on time by the cycle 1->2->3->1, arriving back at he starting location 1 second Before he leaves. He could start from anywhere in the cycle to accomplish this.
Source
Usaco 2006 December Golddetermine if there is a negative ring in the graph.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include < Algorithm> #include <queue>using namespace std;typedef long long LL; #define MAXN 505#define MAXM 5210#define inf 0 X3f3f3f3fint T, N, W, M;int Head[maxn], id;struct Node {int V, W, next;} E[maxm];int OUT[MAXN], dist[maxn];bool vis[maxn];void addedge (int u, int v, int w) {e[id].v = v; E[ID].W = W; E[id].next = Head[u]; Head[u] = id++;} void Getmap () {memset (head,-1, sizeof (int) * (N + 1)); int U, V, W; id = 0; while (m--) {scanf ("%d%d%d", &u, &v, &w); Addedge (U, V, W); Addedge (V, U, W); } while (w--) {scanf ("%d%d%d", &u, &v, &w); Addedge (U, V,-W); }}bool SPFA () {int I, J, U, V, W; Queue<int> Q; for (i = 1; I <= N; ++i) {vis[i] = 1; Out[i] = 0; Dist[i] = 0; Q.push (i); } while (! Q.empty ()) {u = Q.front (); Q.pop (); if (++out[u]> N) return false; Vis[u] = 0; for (i = head[u]; i =-1; i = e[i].next) {v = e[i].v; w = E[I].W; if (Dist[v] > Dist[u] + W) {Dist[v] = Dist[u] + W; if (!vis[v]) {vis[v] = 1; Q.push (v); }}}} return true; int main () {//Freopen ("Stdin.txt", "R", stdin); scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &n, &m, &w); Getmap (); printf (SPFA ()? "no\n": "yes\n"); } return 0;}
POJ3259 wormholes "SPFA judgment negative ring"