The negative ring judgment of the ordinary SPFA. Made three mistakes, all written in the comments.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <algorithm>6 using namespacestd;7 Const intmaxnm=10000+ -;8 Const intmaxnn= -+ -;9 Const intinf=0x7fffffff;Ten intN,m,ww; One intu[maxnm],v[maxnm],w[maxnm]; A intfirst[maxnn],next[maxnm]; - intDis[maxnn]; - voidAddedge (intTuintnum) the { -next[num]=First[tu]; -first[tu]=num; - } + - intSPFA () + { Aqueue<int>que; at intVis[maxnn],appear[maxnn]; -Que.push (0); - for(intI=0; i<n;i++) - { -dis[i]=INF; -vis[i]=0; inappear[i]=0; - } to /*do not omit the above initialization steps*/ +dis[0]=0; -vis[0]=1; theappear[0]=1; * $ while(!que.empty ())Panax Notoginseng { - intHead=Que.front (); the Que.pop (); +vis[head]=0; A /*notice here Vis[head] to be 0 again! It's easy to write Vis[k]=0 .*/ the intk=First[head]; + while(k!=-1) - { $ if(dis[v[k]]>dis[u[k]]+W[k]) $ { -dis[v[k]]=dis[u[k]]+W[k]; - if(!Vis[v[k]]) the /*do not write Vis[v[k] "vis[k].*/ - {Wuyiappear[v[k]]++; thevis[v[k]]=1; - Que.push (V[k]); Wu } - if(appear[v[k]]>=n)return 0; About } $k=Next[k]; - } - } - return 1; A } + the intMain () - { $ intC; thescanf"%d",&c); the for(intKase=0; kase<c;kase++) the { thescanf"%d%d%d",&n,&m,&ww); - for(intI=0; i<n;i++) first[i]=-1; in the for(intI=0; i<m;i++) the { Aboutscanf"%d%d%d",&u[i],&v[i],&w[i]); theu[i]--; thev[i]--; the Addedge (u[i],i); +u[i+m]=V[i]; -v[i+m]=U[i]; thew[i+m]=W[i];BayiAddedge (u[i+m],i+m); the } the for(intI=2*m;i<2*m+ww;i++) - { -scanf"%d%d%d",&u[i],&v[i],&w[i]); theu[i]--; thev[i]--; thew[i]=-W[i]; the Addedge (u[i],i); - } the if(! SPFA ()) cout<<"YES";Elsecout<<"NO";cout<<Endl; the } the 94 return 0; the}
"SPFA" Poj3259-wormhole