1. Introduction:
SPFA algorithm: Single source shortest path algorithm, an efficient shortest path algorithm!
2. Ideas
(1) initialization
1 > Source point path is 0:d[s]=0, where S is the source point
2 > Initialize d[n] to infinity, i.e. d[i], source point s to I for Infinity INF
3>p[n] initialized to the source point s or-1, indicating no precursor
(2) Queue + slack
1> Read the team head point U, and the team head point u out of the team (remember to eliminate the mark);
2> will be relaxed with all point v connected to the point U, and if the estimated value can be updated (even if d[v] becomes smaller), then update;
3> In addition, if Point V is not in the queue, then the point v should be enqueued (remember Mark);
4> if it's already in the queue, then you don't have to go.
In this loop, until the team is empty, the shortest-single source solution is completed.
voidSPFA (intSintt)//s is the source point and T is the end point {inti,j,top,front,rear; for(i=1; i<=n;i++) Dist[i]=inf;//Initialize memset (Vis,0,sizeof(VIS)); Dist[s]=0;//source point path is 0 Vis[s]=1;//visited front=rear=0; Q[rear++]=s;//no precursor . while(front<rear) {Top=q[front++]; Vis[top]=0; for(i=first[top];i!=-1; i=Edge[i].next) {J=edge1[i].to; if(dist[j]>dist[top]+EDGE[I].W)//slack {DIST[J]=dist[top]+EDGE[I].W; if(!Vis[j]) {Vis[j]=1; Q[rear++]=J; } } } }}
SPFA algorithm-Single source Shortest path algorithm