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.
is to ask whether there is a negative ring, so the words must be able to reverse time ...
The code is as follows:
#include <iostream>#include<cstring>#include<cstdio>#include<queue>using namespacestd;Const intinf=10e8;Const intmaxn=510;structedge{intV,cost; Edge (int_v,int_cost): V (_v), cost (_cost) {}};vector<Edge>E[MAXN];BOOLVIS[MAXN];intCOUNODE[MAXN];BOOLSPFA (intLowcost[],intNintstart) {Queue<int>que; intU,v,c; intLen; for(intI=1; i<=n;++i) {Lowcost[i]=INF; Vis[i]=0; Counode[i]=0; } Vis[start]=1; Counode[start]=1; Lowcost[start]=0; Que.push (start); while(!Que.empty ()) {u=Que.front (); Que.pop (); Vis[u]=0; Len=e[u].size (); for(intI=0; i<len;++i) {v=e[u][i].v; C=E[u][i].cost; if(lowcost[u]+c<Lowcost[v]) {Lowcost[v]=lowcost[u]+C; if(!Vis[v]) {Vis[v]=1; ++Counode[v]; Que.push (v); if(counode[v]>=N)return 0; } } } } return 1;} InlinevoidAddedge (intUintVintc) {E[u].push_back (Edge (V,c));}intANS[MAXN];intMain () {intT; intn,m,w; intA,b,c; scanf ("%d",&T); while(t--) {scanf (" %d%d%d",&n,&m,&W); for(intI=1; i<=n;++i) e[i].clear (); for(intI=1; i<=m;++i) {scanf (" %d%d%d",&a,&b,&c); Addedge (A,B,C); Addedge (B,A,C); } for(intI=1; i<=w;++i) {scanf (" %d%d%d",&a,&b,&c); Addedge (A, B,-c); } if(SPFA (Ans,n,1)) printf ("no\n"); Elseprintf ("yes\n"); } return 0;}
View Code
Simple POJ 3259 WORMHOLES,SPFA judgement negative ring.