Bellman-Ford算法

算法

以边为研究对象的最短路算法。

应用场景

  1. 有负边权的最短路问题。

  2. 负环的判定。

算法原理

  1. \(n\) 个点的最短路径最多经过 \(n - 1\) 条边。

  2. 每条边要么经过,要么不经过,对于一条边的两个端点 \(x\) 和 \(y\)。

    所以我们把每一条边都枚举一遍看是否可以进行松弛。

  3. 将步骤 \(2\) 重复 \(n - 1\) 轮后,\(dis[i]\) 即为起点到达点 \(i\) 的最短路。

代码:

for (int i = 1; i <= n - 1; i++)
for (int j = 1; j <= m; j++)
if (dis[e[j].x] + e[j].w < dis[e[j].y])
dis[e[j].y] = dis[e[j].x] + e[j].w;

时间复杂度

显然是\(O(nm)\)。

Bellman-Ford算法的队列优化(SPFA)

  1. 一个点只会松弛它的邻接点,因此,只有 \(x\) 的邻接点的 \(y\) 被松驰过,点 \(x\) 才有松弛的必要。

  2. 用队列维护被松弛过的点集。

  3. 对于随机数据优化明显,但是最坏情况时间复杂度仍然为 \(O(nm)\)。

模板:Luogu P3371

1.SPFA代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <queue> using namespace std; const int N = 10010, M = 500010, INF = 0x3f3f3f3f; struct Edge
{
int to;
int next;
int w;
}e[M]; int head[N], idx; void add(int a, int b, int c)
{
idx++, e[idx].to = b, e[idx].next = head[a], e[idx].w = c, head[a] = idx;
} int n, m, s;
int dis[N]; void spfa(int u)
{
queue<int> q;
q.push(u);
memset(dis, 0x3f, sizeof(dis));
dis[u] = 0; while (q.size())
{
int t = q.front();
q.pop(); for (int i = head[t]; i; i = e[i].next)
{
int to = e[i].to;
if (dis[to] > dis[t] + e[i].w)
{
dis[to] = dis[t] + e[i].w;
q.push(to);
}
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cin >> n >> m >> s; for (int i = 1; i <= m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
} spfa(s); for (int i = 1; i <= n; i++)
{
if (dis[i] == INF) cout << INT_MAX << ' ';
else cout << dis[i] << ' ';
}
return 0;
}

2. 优化1:加一个st数组,防止元素重复进队列。

快了215ms

重点关注第27,35,41,49,50,51,52,53行。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <queue> using namespace std; const int N = 10010, M = 500010, INF = 0x3f3f3f3f; struct Edge
{
int to;
int next;
int w;
}e[M]; int head[N], idx; void add(int a, int b, int c)
{
idx++, e[idx].to = b, e[idx].next = head[a], e[idx].w = c, head[a] = idx;
} int n, m, s;
int dis[N];
bool st[N]; void spfa(int u)
{
queue<int> q;
q.push(u);
memset(dis, 0x3f, sizeof(dis));
dis[u] = 0;
st[u] = true; while (q.size())
{
int t = q.front();
q.pop();
st[t] = false; for (int i = head[t]; i; i = e[i].next)
{
int to = e[i].to;
if (dis[to] > dis[t] + e[i].w)
{
dis[to] = dis[t] + e[i].w;
if (!st[to])
{
st[to] = true;
q.push(to);
}
}
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cin >> n >> m >> s; for (int i = 1; i <= m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
} spfa(s); for (int i = 1; i <= n; i++)
{
if (dis[i] == INF) cout << INT_MAX << ' ';
else cout << dis[i] << ' ';
}
return 0;
}

3. 优化2:SLF 优化

每次压入队列时,是看放后面还是前面,所以需要双端队列。

快了18ms

重点关注代码第31,52,53行。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <queue> using namespace std; const int N = 10010, M = 500010, INF = 0x3f3f3f3f; struct Edge
{
int to;
int next;
int w;
}e[M]; int head[N], idx; void add(int a, int b, int c)
{
idx++, e[idx].to = b, e[idx].next = head[a], e[idx].w = c, head[a] = idx;
} int n, m, s;
int dis[N];
bool st[N]; void spfa(int u)
{
deque<int> q;
q.emplace_back(u);
memset(dis, 0x3f, sizeof(dis));
dis[u] = 0;
st[u] = true; while (q.size())
{
int t = q.front();
q.pop_front();
st[t] = false; for (int i = head[t]; i; i = e[i].next)
{
int to = e[i].to;
if (dis[to] > dis[t] + e[i].w)
{
dis[to] = dis[t] + e[i].w;
if (!st[to])
{
st[to] = true;
if (q.size() && dis[q.front()] >= dis[to]) q.emplace_front(to);
else q.emplace_back(to);
}
}
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cin >> n >> m >> s; for (int i = 1; i <= m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
} spfa(s); for (int i = 1; i <= n; i++)
{
if (dis[i] == INF) cout << INT_MAX << ' ';
else cout << dis[i] << ' ';
}
return 0;
}

4. 优化3:此优化仅针对不开O2优化时。

使用循环队列进行优化(我改编的)。

(开了O2优化效果不明显,亲自测试在跑网络流时快了800ms,TLE->AC)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <queue> using namespace std; const int N = 10010, M = 500010, INF = 0x3f3f3f3f; struct Edge
{
int to;
int next;
int w;
}e[M]; int head[N], idx; void add(int a, int b, int c)
{
idx++, e[idx].to = b, e[idx].next = head[a], e[idx].w = c, head[a] = idx;
} int n, m, s;
int dis[N];
bool st[N]; int q[N];
int hh, tt;
int sz; void push_back(int x)
{
sz++;
q[tt++] = x;
if (tt == N) tt = 0;
} void push_front(int x)
{
sz++;
hh--;
if (hh == -1) hh = N - 1;
q[hh] = x;
} void pop_back()
{
sz--;
tt--;
if (tt == -1) tt = N - 1;
} void pop_front()
{
sz--;
hh++;
if (hh == N) hh = 0;
} void spfa(int u)
{
push_back(u);
memset(dis, 0x3f, sizeof(dis));
dis[u] = 0;
st[u] = true; while (sz)
{
int t = q[hh];
pop_front();
st[t] = false; for (int i = head[t]; i; i = e[i].next)
{
int to = e[i].to;
if (dis[to] > dis[t] + e[i].w)
{
dis[to] = dis[t] + e[i].w;
if (!st[to])
{
st[to] = true;
if (sz && dis[q[hh]] >= dis[to]) push_front(to);
else push_back(to);
}
}
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cin >> n >> m >> s; for (int i = 1; i <= m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
} spfa(s); for (int i = 1; i <= n; i++)
{
if (dis[i] == INF) cout << INT_MAX << ' ';
else cout << dis[i] << ' ';
}
return 0;
}

5. 优化4:LLL优化

如果当前队首元素 \(t\),队列长度为 \(size\),队列里各元素和为 \(sum\) ,有 \(dis[t] \times size \leq sum\),才取队首。

否则把队首元素压入队尾并弹出队首。

效果好像不怎么明显

重点关注代码的第68,72,73,74,75,76,91行。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <queue> using namespace std; const int N = 10010, M = 500010, INF = 0x3f3f3f3f; struct Edge
{
int to;
int next;
int w;
}e[M]; int head[N], idx; void add(int a, int b, int c)
{
idx++, e[idx].to = b, e[idx].next = head[a], e[idx].w = c, head[a] = idx;
} int n, m, s;
int dis[N];
bool st[N]; int q[N];
int hh, tt;
int sz; void push_back(int x)
{
sz++;
q[tt++] = x;
if (tt == N) tt = 0;
} void push_front(int x)
{
sz++;
hh--;
if (hh == -1) hh = N - 1;
q[hh] = x;
} void pop_back()
{
sz--;
tt--;
if (tt == -1) tt = N - 1;
} void pop_front()
{
sz--;
hh++;
if (hh == N) hh = 0;
} void spfa(int u)
{
push_back(u);
memset(dis, 0x3f, sizeof(dis));
dis[u] = 0;
st[u] = true;
int sum = dis[u]; while (sz)
{
while (dis[q[hh]] * sz > sum)
{
push_back(q[hh]);
pop_front();
}
int t = q[hh];
pop_front();
st[t] = false;
sum -= dis[t]; for (int i = head[t]; i; i = e[i].next)
{
int to = e[i].to;
if (dis[to] > dis[t] + e[i].w)
{
dis[to] = dis[t] + e[i].w;
if (!st[to])
{
st[to] = true;
sum += dis[to];
if (sz && dis[q[hh]] >= dis[to]) push_front(to);
else push_back(to);
}
}
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cin >> n >> m >> s; for (int i = 1; i <= m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
} spfa(s); for (int i = 1; i <= n; i++)
{
if (dis[i] == INF) cout << INT_MAX << ' ';
else cout << dis[i] << ' ';
}
return 0;
}

本文终

Bellman-Ford算法及SPFA算法的思路及进一步优化的更多相关文章

  1. 数据结构与算法--最短路径之Bellman算法、SPFA算法

    数据结构与算法--最短路径之Bellman算法.SPFA算法 除了Floyd算法,另外一个使用广泛且可以处理负权边的是Bellman-Ford算法. Bellman-Ford算法 假设某个图有V个顶点 ...

  2. 最短路径——Bellman-Ford算法以及SPFA算法

    说完dijkstra算法,有提到过朴素dij算法无法处理负权边的情况,这里就需要用到Bellman-Ford算法,抛弃贪心的想法,牺牲时间的基础上,换取负权有向图的处理正确. 单源最短路径 Bellm ...

  3. Bellman-ford算法、SPFA算法求解最短路模板

    Bellman-ford 算法适用于含有负权边的最短路求解,复杂度是O( VE ),其原理是依次对每条边进行松弛操作,重复这个操作E-1次后则一定得到最短路,如果还能继续松弛,则有负环.这是因为最长的 ...

  4. Bellman-Ford算法与SPFA算法详解

    PS:如果您只需要Bellman-Ford/SPFA/判负环模板,请到相应的模板部分 上一篇中简单讲解了用于多源最短路的Floyd算法.本篇要介绍的则是用与单源最短路的Bellman-Ford算法和它 ...

  5. 最短路径算法 4.SPFA算法(1)

    今天所说的就是常用的解决最短路径问题最后一个算法,这个算法同样是求连通图中单源点到其他结点的最短路径,功能和Bellman-Ford算法大致相同,可以求有负权的边的图,但不能出现负回路.但是SPFA算 ...

  6. 图论之最短路算法之SPFA算法

    SPFA(Shortest Path Faster Algorithm)算法,是一种求最短路的算法. SPFA的思路及写法和BFS有相同的地方,我就举一道例题(洛谷--P3371 [模板]单源最短路径 ...

  7. 最短路径算法之四——SPFA算法

    SPAF算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm,该算法是西南交通大学段凡丁于1994年发表的. 它可以在O(kE)的时间复杂度内求出源点 ...

  8. 最短路径问题---Floyed(弗洛伊德算法),dijkstra算法,SPFA算法

    在NOIP比赛中,如果出图论题最短路径应该是个常考点. 求解最短路径常用的算法有:Floyed算法(O(n^3)的暴力算法,在比赛中大概能过三十分) dijkstra算法 (堆优化之后是O(MlogE ...

  9. hdoj 1874 畅通工程续【dijkstra算法or spfa算法】

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  10. Bellman-ford算法与SPFA算法思想详解及判负权环(负权回路)

    我们先看一下负权环为什么这么特殊:在一个图中,只要一个多边结构不是负权环,那么重复经过此结构时就会导致代价不断增大.在多边结构中唯有负权环会导致重复经过时代价不断减小,故在一些最短路径算法中可能会凭借 ...

随机推荐

  1. vs的常用配置【以及vs常用的快捷键】

    1.颜色设置 (1) 编译器的主题颜色设置 (2) 字体和颜色设置 (3) 字体大小 更快捷的修改字体大小方式:ctr+鼠标滚轮 2.行号设置 默认就有,不用设置了 3.把解决方案资源管理器移动到左边 ...

  2. C++核心知识回顾(函数&参数、异常、动态分配)

    复习C++的核心知识 函数与参数 传值参数.模板函数.引用参数.常量引用参数 传值参数 int abc(int a,int b,int c) { return a + b * c; } a.b.c是函 ...

  3. vue常用标签(引入vue.js写法)

    首先在html中引入vue.js,具体怎么下载可以参考https://blog.csdn.net/lvoelife/article/details/129254906,下载后在html中引入: 一 内 ...

  4. 人工智能AI图像风格迁移(StyleTransfer),基于双层ControlNet(Python3.10)

    图像风格迁移(Style Transfer)是一种计算机视觉技术,旨在将一幅图像的风格应用到另一幅图像上,从而生成一幅新图像,该新图像结合了两幅原始图像的特点,目的是达到一种风格化叠加的效果,本次我们 ...

  5. GPT-NER:通过大型语言模型的命名实体识别

    讲在前面,chatgpt出来的时候就想过将其利用在信息抽取方面,后续也发现了不少基于这种大语言模型的信息抽取的论文,比如之前收集过的: https://github.com/cocacola-lab/ ...

  6. 2023-04-28:将一个给定字符串 s 根据给定的行数 numRows 以从上往下、从左到右进行 Z 字形排列 比如输入字符串为 “PAYPALISHIRING“ 行数为 3 时,排列如下 P

    2023-04-28:将一个给定字符串 s 根据给定的行数 numRows 以从上往下.从左到右进行 Z 字形排列 比如输入字符串为 "PAYPALISHIRING" 行数为 3 ...

  7. 2021-04-18:给定一个二维数组matrix,里面的值不是1就是0,上、下、左、右相邻的1认为是一片岛,返回matrix中岛的数量。

    2021-04-18:给定一个二维数组matrix,里面的值不是1就是0,上.下.左.右相邻的1认为是一片岛,返回matrix中岛的数量. 福大大 答案2021-04-18: 并查集. 代码用gola ...

  8. Vue 全局避免按钮重复点击

    这里用到的 Vue.directive 自定义指令 自定义指令是对普通DOM元素进行的底层操作,它是一种有效的的补充和扩展,不仅可以用于定义任何的dom操作,并且是可以复用的 在 main.js 中写 ...

  9. 【GiraKoo】Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)

    [解决]Could NOT find PkgConfig (missing PKG_CONFIG_EXECUTABLE) 环境 Ubuntu 22.04 现象 在编写CMakeLists.txt时,调 ...

  10. T-SQL——批量刷新视图

    目录 0. 背景说明 1. 查询出所有使用了指定表的视图并生成刷新语句 2. 创建存储过程批量刷新 3. 刷新全部的视图 4. 参考 shanzm--2023年5月16日 0. 背景说明 为什么要刷新 ...