全称:Shortest Path Faster Algorithm
对于Bellman-ford算法,可用于来求含有负权边的最短路径,对于n个结点m条边的图,时间复杂度达到了,而SPFA算法,极限情况下虽然时间复杂度也是
, 但是在某些情况下对时间做出了优化(队列优化)
对于Bellman-ford算法,是用当前阶段的所有边去更新与每条边存在有向关系的边(内部更新),显然需要一个一个处理,SPFA的总体思路是:建立一个队列,队列中存放经过更新的点,每次取出对头,领接表遍历和这个点存在有向边的点,如果被更新了,并且队列中未出现该点,则放入队列。
核心思想:这个算法优化之处在于对于被更新的点,如果说这个点存在于队列中时,边此时已被更新,不需要再次入队,和传统的Bellman-ford算法不同的是:Bellman-ford如果一个点被更新,那么这个点还需要重新一个一个去更新所有结点,显然SPFA要快上许多.
此外:这个算法还可以求只包含非负权边的图(dijkstra算法).
相关头文件
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
关于一些变量的处理(n表示点数,m表示边数,邻接表建图,dist表示最短距离,w表示边权,str判断是否入队):
const int N = 1e5 + 10;
int n, m;
int e[N], idx, ne[N], h[N], w[N];
int dist[N];
bool str[N];
邻接表建图:
void add(int a, int b, int c){
w[idx] = c, e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
SPFA代码如下:
void spfa(){
memset(dist, 0x3f, sizeof dist);
queue<int> q; // 存结点
dist[1] = 0;
q.push(1);
str[1] = true;
while(q.size()){
auto t = q.front();
q.pop();
str[t] = false;
for (int i = h[t]; ~ i; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i]; //更新操作,如果在队列中,自动更新,反之,更新 + 入队
if(!str[j]){
q.push(j);
str[j] = true;
}
}
}
}
return;
}
主函数如下:
int main(){
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 1; i <= m; i ++ ){
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
spfa();
if(dist[n] > 0x3f3f3f3f / 2) puts("impossible");
else cout << dist[n] << endl;
return 0;
}
全部如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int e[N], idx, ne[N], h[N], w[N];
int dist[N];
bool str[N];
void add(int a, int b, int c){
w[idx] = c, e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void spfa(){
memset(dist, 0x3f, sizeof dist);
queue<int> q; // 存结点
dist[1] = 0;
q.push(1);
str[1] = true;
while(q.size()){
auto t = q.front();
q.pop();
str[t] = false;
for (int i = h[t]; ~ i; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i]; //更新操作,如果在队列中,自动更新,反之,更新 + 入队
if(!str[j]){
q.push(j);
str[j] = true;
}
}
}
}
return;
}
int main(){
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 1; i <= m; i ++ ){
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
spfa();
if(dist[n] > 0x3f3f3f3f / 2) puts("impossible");
else cout << dist[n] << endl;
return 0;
}
此外,SPFA算法还可以判断图中是否有负环
该算法在算法竞赛上可以替代dijkstra算法,时间复杂度相比dijkstra来说,普遍意义上更快。
补充:由于是判断图中是否有非负环,所以需要将所有结点插入,注意需要引入cnt数组用来记录更显多少次,显然倘若cnt大于等于n时满足条件。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int e[N], idx, ne[N], h[N], w[N];
int dist[N];
bool str[N];
int cnt[N];
void add(int a, int b, int c){
w[idx] = c, e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
int spfa(){
memset(dist, 0x3f, sizeof dist);
queue<int> q; // 存结点
for(int i = 1; i <= n; i ++ ){
str[i] = true; //由于是判断图中是否有非负环,所以需要将所有结点插入
q.push(i);
}
while(q.size()){
auto t = q.front();
q.pop();
str[t] = false;
for (int i = h[t]; ~ i; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i]; //更新操作,如果在队列中,自动更新,反之,更新 + 入队
cnt[j] = cnt[t] + 1;
if(cnt[j] >= n) return true;
if(!str[j]){
q.push(j);
str[j] = true;
}
}
}
}
return false;
}
int main(){
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 1; i <= m; i ++ ){
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
bool st = spfa();
if(st)puts("Yes");
else puts("No");
return 0;
}