bzoj1715[usaco2006 Dec]wormholes Insect Cave
Test instructions
Determine if a graph has a negative ring. Points ≤500, number of sides ≤3000. (I do not understand the original question, and then read the problem, it is directly said)
Exercises
SPFA If a point has been updated more than n times, then there is a negative ring in this diagram.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineMAXN 5107 #defineINF 0X3FFFFFFF8 using namespacestd;9 TenInlineintRead () { One CharCh=getchar ();intf=1, x=0; A while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); - returnf*x; the } - structe{intT,w,n;} es[maxn* the];intg[maxn],ess; - voidPeintFintTintW) {es[++ess]= (e) {t,w,g[f]}; g[f]=ess;} - intF,N,M,W,D[MAXN],CNT[MAXN];BOOLINQ[MAXN]; queue<int>Q; + BOOLSPFA () { - while(!q.empty ()) Q.pop (); Inc (I,1, N) d[i]=inf,cnt[i]=inq[i]=0; d[1]=0; inq[1]=1; Q.push (1); + while(!Q.empty ()) { A intX=q.front (); Q.pop (); inq[x]=0; at for(intI=G[X];I;I=ES[I].N)if(d[es[i].t]>d[x]+ES[I].W) { -D[ES[I].T]=D[X]+ES[I].W; cnt[es[i].t]++;if(cnt[es[i].t]>n)return 1; - if(!inq[es[i].t]) Q.push (es[i].t), inq[es[i].t]=1; - } - } - return 0; in } - intMain () { tof=read (); + while(f--){ -N=read (); M=read (); W=read (); Memset (G,0,sizeof(g)); ess=0; theInc (I,1, m) {intX=read (), Y=read (), z=read (); PE (x, y, z); PE (Y,X,Z);} *Inc (I,1, W) {intX=read (), Y=read (), Z=read (); PE (x,y,-z);} $ if(SPFA ()) puts ("YES");ElsePuts"NO");Panax Notoginseng } - return 0; the}
20160912
bzoj1715[usaco2006 Dec]wormholes Wormhole *