"SPFA" Poj3259-wormhole

Source: Internet
Author: User

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

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.