SPFA及SLF优化
2012-08-12 07:54
323 查看
算法简介
SPFA(Shortest Path Faster Algorithm)是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算。 它可以在O(kE)的时间复杂度内求出源点到其他所有点的最短路径,可以处理负边。
算法流程
SPFA对Bellman-Ford算法优化的关键之处在于意识到:只有那些在前一遍松弛中改变了距离估计值的点,才可能引起他们的邻接点的距离估计值的改变。因此,算法大致流程是用一个队列来进行维护,即用一个先进先出的队列来存放被成功松弛的顶点。初始时,源点s入队。当队列不为空时,取出队首顶点, 对它的邻接点进行松弛。如果某个邻接点松弛成功,且该邻接点不在队列中,则将其入队。经过有限次的松弛操作后,队列将为空,算法结束。SPFA算法的实现,需要用到一个先进先出的队列queue 和一个指示顶点是否在队列中的标记数组mark。为了方便查找某个顶点的邻接点,图采用邻接表存储。
判断负权回路的方案很多,世间流传最广、比较容易实现并且高效的方法的是记录每个结点进队次数,大于等于|V|次表示有负权。
两个著名优化(SLF和LLL):
SPFA 是按照 FIFO 的原则更新距离的, 没有考虑到距离标号的作用. 实现中 SPFA 有两个非常著名的优化: SLF 和 LLL.
SLF: Small Label First 策略. (比较常用)
实现方法是, 设队首元素为
![](http://data.artofproblemsolving.com/images/latex/0/4/2/042dc4512fa3d391c5170cf3aa61e6a638f84342.gif)
, 队列中要加入节点
![](http://data.artofproblemsolving.com/images/latex/5/c/2/5c2dd944dde9e08881bef0894fe7b22a5c9c4b06.gif)
, 在
![](http://data.artofproblemsolving.com/images/latex/7/4/c/74cf276f40fc8ea054c55cbf77c0f87ac2780880.gif)
时加到队首而不是队尾, 否则和普通的 SPFA 一样加到队尾.
LLL: Large Label Last 策略. (不太常用)
实现方法是, 设队列
![](http://data.artofproblemsolving.com/images/latex/c/3/1/c3156e00d3c2588c639e0d3cf6821258b05761c7.gif)
中的队首元素为
![](http://data.artofproblemsolving.com/images/latex/0/4/2/042dc4512fa3d391c5170cf3aa61e6a638f84342.gif)
, 距离标号的平均值为
![](http://data.artofproblemsolving.com/images/latex/2/f/a/2fa7b8e04e7f0f96cc4212d584cd36ecf0734115.gif)
, 每次出队时, 若
![](http://data.artofproblemsolving.com/images/latex/8/b/3/8b3ae187ffe26b5bc222d3732711111a83a9fe00.gif)
, 把
![](http://data.artofproblemsolving.com/images/latex/0/4/2/042dc4512fa3d391c5170cf3aa61e6a638f84342.gif)
移到队列末尾, 如此反复, 直到找到一个
![](http://data.artofproblemsolving.com/images/latex/0/4/2/042dc4512fa3d391c5170cf3aa61e6a638f84342.gif)
使
![](http://data.artofproblemsolving.com/images/latex/7/5/8/758d48935f8d0ca3092e859cd01858e059df2cf7.gif)
, 将其出队.
C++模版:(没加SLF优化)
SPFA(Shortest Path Faster Algorithm)是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算。 它可以在O(kE)的时间复杂度内求出源点到其他所有点的最短路径,可以处理负边。
算法流程
SPFA对Bellman-Ford算法优化的关键之处在于意识到:只有那些在前一遍松弛中改变了距离估计值的点,才可能引起他们的邻接点的距离估计值的改变。因此,算法大致流程是用一个队列来进行维护,即用一个先进先出的队列来存放被成功松弛的顶点。初始时,源点s入队。当队列不为空时,取出队首顶点, 对它的邻接点进行松弛。如果某个邻接点松弛成功,且该邻接点不在队列中,则将其入队。经过有限次的松弛操作后,队列将为空,算法结束。SPFA算法的实现,需要用到一个先进先出的队列queue 和一个指示顶点是否在队列中的标记数组mark。为了方便查找某个顶点的邻接点,图采用邻接表存储。
判断负权回路的方案很多,世间流传最广、比较容易实现并且高效的方法的是记录每个结点进队次数,大于等于|V|次表示有负权。
两个著名优化(SLF和LLL):
SPFA 是按照 FIFO 的原则更新距离的, 没有考虑到距离标号的作用. 实现中 SPFA 有两个非常著名的优化: SLF 和 LLL.
SLF: Small Label First 策略. (比较常用)
实现方法是, 设队首元素为
![](http://data.artofproblemsolving.com/images/latex/0/4/2/042dc4512fa3d391c5170cf3aa61e6a638f84342.gif)
, 队列中要加入节点
![](http://data.artofproblemsolving.com/images/latex/5/c/2/5c2dd944dde9e08881bef0894fe7b22a5c9c4b06.gif)
, 在
![](http://data.artofproblemsolving.com/images/latex/7/4/c/74cf276f40fc8ea054c55cbf77c0f87ac2780880.gif)
时加到队首而不是队尾, 否则和普通的 SPFA 一样加到队尾.
LLL: Large Label Last 策略. (不太常用)
实现方法是, 设队列
![](http://data.artofproblemsolving.com/images/latex/c/3/1/c3156e00d3c2588c639e0d3cf6821258b05761c7.gif)
中的队首元素为
![](http://data.artofproblemsolving.com/images/latex/0/4/2/042dc4512fa3d391c5170cf3aa61e6a638f84342.gif)
, 距离标号的平均值为
![](http://data.artofproblemsolving.com/images/latex/2/f/a/2fa7b8e04e7f0f96cc4212d584cd36ecf0734115.gif)
, 每次出队时, 若
![](http://data.artofproblemsolving.com/images/latex/8/b/3/8b3ae187ffe26b5bc222d3732711111a83a9fe00.gif)
, 把
![](http://data.artofproblemsolving.com/images/latex/0/4/2/042dc4512fa3d391c5170cf3aa61e6a638f84342.gif)
移到队列末尾, 如此反复, 直到找到一个
![](http://data.artofproblemsolving.com/images/latex/0/4/2/042dc4512fa3d391c5170cf3aa61e6a638f84342.gif)
使
![](http://data.artofproblemsolving.com/images/latex/7/5/8/758d48935f8d0ca3092e859cd01858e059df2cf7.gif)
, 将其出队.
C++模版:(没加SLF优化)
#include <fstream> #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <iomanip> #include <iomanip> #include <climits> #include <vector> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <algorithm> #include <string> #include <cstring> using namespace std; #define arraysize 501 int maxData = 0x7fffffff; typedef struct edge { int to; int w; }edge; vector<edge> adjmap[arraysize]; //vector实现邻接表 int d[arraysize]; bool final[arraysize]; //记录顶点是否在队列中,SPFA算法可以入队列多次 int cnt[arraysize]; //记录顶点入队列次数 bool SPFA(int s) { queue<int> myqueue; int i,j; for(i=0;i<n+1;++i) { d[i] = maxData; //将除源点以外的其余点的距离设置为无穷大 } memset(final,0,sizeof(final)); memset(cnt,0,sizeof(cnt)); d[s]=0; //源点的距离为0 final[s] = true; cnt[s]++; //源点的入队列次数增加 myqueue.push(s); int topint; while(!myqueue.empty()) { topint = myqueue.front();myqueue.pop(); final[topint] = false; for(i=0;i<adjmap[topint].size();++i) { int to = adjmap[topint][i].to; if(d[topint]<maxData && d[to]>d[topint]+ adjmap[topint][i].w) { d[to] = d[topint]+ adjmap[topint][i].w; if(!final[to]) { final[to] = true; cnt[to]++; if(cnt[to]>=n) //当一个点入队的次数>=n时就证明出现了负环。 return true; myqueue.push(to); } } } } return false; } int main() { for(i=1;i<n+1;++i) //此处特别注意对邻接表清空 adjmap[i].clear(); for(i=0;i<m;++i) //双向 { cin>>s>>e>>w; temp.to = e; temp.w = w; adjmap[s].push_back(temp); temp.to = s; adjmap[e].push_back(temp); } }
相关文章推荐
- 单源最短路----Spfa模板 (SLF优化)
- [SPFA的SLF优化] Codeforces Round #257 (Div. 1) B
- bzoj2100 [Usaco2010 Dec]Apple Delivery(slf优化的spfa)
- SPFA及SLF优化
- 51nod1459 迷宫游戏 spfa的slf优化
- Silver Cow Party SPFA+SLF优化 单源起点终点最短路
- spfa的SLF优化
- SPFA的两种优化SLF和LLL
- Codeforces Round #257 (Div. 1)B题Jzzhu and Cities(spfa+slf优化)
- 【codevs】2273 扬帆远洋大战牧师妹酱 SPFA slf 优化
- spfa优化 SLF LLL
- 2017.7.3 SPFA SLF优化
- HDOJ 4889 Scary Path Finding Algorithm 颠覆spfa slf优化
- SPFA优化:SLF,LLL,前向星
- 算法提高 道路和航路 (SPFA的SLF优化)
- SPFA模板(SLF优化)
- BSOJ:2636 收费站--二分+SPFA(SLF优化)
- SPFA的LLL和SLF(以及自己浪出来的PY优化,不知道有没有别人写过QAQ)
- 2013成都邀请赛J题||HDU4725 The Shortest Path in Nya Graph(spfa+slf优化最短路)
- 2346: [Baltic 2011]Lamp (SPFA+SLF优化)