SPFA Ordinary version will not write, optimization or to the ANG, SPFA can be sentenced to negative ring, accept negative right side and heavy edge, judge negative ring only need to open an array to record each node of the number of times, when there is any one node more than a point on the queue indicates that there is a negative ring exists
1#include <stdio.h>//SPFA basically want these header files2#include <string.h>3#include <queue>4 using namespacestd;5 6 voidSPFA (intSintp) {7memset (Vis,0,sizeof(Vis));8memset (dist,-1,sizeof(Dist));9queue<int>Q;Tenvis[s]=1; Onedist[s]=0; A Q.push (s); - while(!Q.empty ()) { - inti,t=Q.front (); thevis[t]=0; - Q.pop (); - for(i=head[t];~i;i=Next[i]) { - intj=Point[i]; + if(dist[j]==-1|| dist[j]>dist[t]+Val[i]) { -dist[j]=dist[t]+Val[i]; + if(!Vis[j]) { A Q.push (j); atvis[j]=1; - } - } - } - } -printf"%d\n", Dist[p]); in}
Shortest path--spfa+ Queue optimization template