1、Bellman-Ford算法是用來處理圖中存在負權邊的最短路情況,當圖中有負權邊時,Dijkstra()就不能用了,計算出的最短路會有問題。這裏要注意若圖中存在負權迴路,最短路很可能不存在(在負權迴路不影響我們想要走過的路徑時,不影響結果),Bellman-Ford算法的思路是非常簡單的,其應用場景也比較有限。
首先在進行鬆弛操作的時候,需要注意,要用上一次更新過的距離來更新其他節點,即需要對上一次的距離做一個保存,這樣做的原因是在距離的更新過程中可能會發生串聯,而BF算法一個重要的使用場景就是處理對經過的邊數有限制的最短路問題,發生串聯可能會導致錯誤。
這裏對經過的邊數有限制的最短路問題可以舉一個具體的實例,比如我們在旅遊時經常無法直達,需要換成,這裏假設你可能要換乘的次數比較多,每一次的換乘就是一條邊,價錢就是權重,你需要一條總價錢最小的換乘路線,但是又有一個因素會產生影響,就是每一次換乘都會影響你的心情,所以你希望換乘的次數是有限的,對應的就是對最短路的邊數有限制。
代碼實例:一個n個點,m條邊的有向圖,邊權可能爲負數,求出從一號點到n號點的最多經過k條邊的最短距離。
這裏使用結構體來存邊
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 510, M = 10010;
struct Edge //使用結構體存邊
{
int a, b, c;
}edges[M];
int n, m, k;
int dist[N];
int last[N];
void bellman_ford()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0; //選一個起點
for(int i = 0; i < k; i ++) //k爲限制的邊數
{
memcpy(last, dist, sizeof dist);
for(int j = 0; j < m; j ++) //每次循環都會去更新一次所有邊
{
auto e = edges[j];
dist[e.b] = min(dist[e.b], last[e.a] + e.c); //這裏更新使用的上一次的狀態
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
for(int i = 0; i < m; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
edges[i] = {a, b, c};
}
bellman_ford();
if(dist[n] > 0x3f3f3f3f / 2) puts("impossible");
else cout << dist[n] << endl;
return 0;
}
2、SPFA算法
這個算法是對BF算法的一個優化,在圖中沒有負環的情況下可用,從我們觀察上面的代碼可以看出每一次循環,都會更新所有的邊,但其實這是不需要的,因爲只有當前點的距離被更新,也就是說距離變小,才需要去更新這個的出邊所連接的點,這個算法有些像是Dijkstra算法,事實上很多隻有正權邊的問題也可以用SPFA來做。它的時間複雜度爲O(m),最壞情況下爲O(nm)。
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100010;
int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N];
bool st[N]; //存儲每個點是否在隊列中
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
//時間複雜度O(m)
int spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
queue<int> q;
q.push(1);
st[1] = true;
while(q.size()) //隊列中存的是所有待更新的點
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; i != -1; i = ne[i]) //遍歷這個點的所有出邊連接的點
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if(!st[j]) //若隊列中已經存在j,則不需要將j重複插入,避免重複入隊
{
q.push(j);
st[j] = true;
}
}
}
}
return dist[n];
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while(m --)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
int t = spfa();
if(t == 0x3f3f3f3f) puts("impossible");
else printf("%d\n", t);
return 0;
}
SPFA的一個重要應用,判斷圖中是否存在負環
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2010, M = 100010;
int n, m;
int h[N], w[M], e[M], ne[M], idx;
int dist[N], cnt[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa()
{
queue<int> q;
for(int i = 1; i <= n; i++) //所有節點入隊並進行標記
{
st[i] = true;
q.push(i);
}
while(q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; i != -1; 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(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while(m --)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
if(spfa()) puts("Yes");
else puts("No");
return 0;
}