转自:https://blog.csdn.net/oranges_c/article/details/64124235
这个博客写的比较清楚
SPFA算法有两个优化算法 SLF 和 LLL:
SLF:Small Label First 策略,设要加入的节点是j,队首元素为i,若dist(j) < dist(i),则将j插入队首,否则插入队尾。
LLL:Large Label Last 策略,设队首元素为i,每次弹出时进行判断,队列中所有dist值的平均值为x,若dist(i)>x则将i插入到队尾,查找下一元素,直到找到某一i使得dist(i)<=x,则将i出对进行松弛操作。
SLF:dist[v[e]] < dist[queue[head]] 这句话很关键
for (int e = first0[k]; e!=-1; e=next0[e]){
if (dist[k] + w[e] < dist[v[e]]) {
dist[v[e]] = dist[k] + w[e];
if (!book[v[e]]) {
if (head != tail && dist[v[e]] < dist[queue[head]] && head>=1) {
queue[--head] = v[e];
}else{
queue[tail++] = v[e];
book[v[e]] = 1;
}
}
}
}