wormholes
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 36641 |
|
Accepted: 13405 |
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 toF (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
Title Link: http://poj.org/problem?id=3259
The main topic: time travel, the former M road is two-way, travel time is positive, W road is Worm hole, one-way, travel time is negative, that is, can go back to the past. From a point of departure, to determine whether in the "past" back to the starting point, that will be the starting point of time is negative.
Problem-solving ideas: bare negative right the shortest, SPAF solve. Enumerate the starting point.
The code is as follows:
#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm > #define INF 1e9using namespace Std;int const maxn=505;int N,p;int DIST[MAXN],VIS[MAXN],NUM[MAXN]; struct Node {int v,w;node (int vv,int ww) {v=vv;w=ww;}}; Vector <node> vt[maxn];void SPAF (int v0) {memset (vis,0,sizeof (VIS)); memset (num,0,sizeof (num)); for (int i=0;i<maxn;i++) Dist[i]=-inf;queue <int>q;q.push (v0);d ist[v0]=0; while (!q.empty ()) {int U=q.front (); Q.pop (); if (num[u]>n) continue;num[u]++;if (num[u]>=n) dist[u]=inf;vis[u]=0 ; int len=vt[u].size (); for (int i=0;i<len;i++) {int v=vt[u][i].v;int w=vt[u][i].w;if (dist[v]<dist[u]+w) {dist[v]=dist[u]+w;if (v==v0& amp;&dist[v]>0) {P=1;return;} if (!vis[v]) {Vis[v]=1;q.push (v);}}}} int main (void) {int m1,m2,u,v,w,t;scanf ("%d", &t), while (t--) {scanf ("%d%d%d", &n,&m1,&m2); int ans=0;p =0;for (int i=0;i<maxn;i++) vt[i].clear (); for (int i=0;i<m1;i++) {scanf ("%d%d%d", &amP;U,&V,&W); Bidirectional path Vt[u].push_back (node (v,-w)); Vt[v].push_back (node (u,-w));} for (int i=0;i<m2;i++) {scanf ("%d%d%d", &u,&v,&w); Vt[u].push_back (node (v,w)); One-way path}for (int i=1;i<=n;i++) {SPAF (i), if (p) break;} if (p) printf ("yes\n"); elseprintf ("no\n");}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3259 wormholes (negative right shortest way, SPAF)