最短路問題 - 模板總結(Dijkstra + Bellman-ford + SPFA + Floyd)
文章目錄
最短路問題分類圖:
1、Dijkstra算法(正邊權單源最短路問題)
1-1、樸素Dijkstra算法-O(n2)
給定一個n個點m條邊的有向圖,圖中可能存在重邊和自環,所有邊權均爲正值。
請你求出1號點到n號點的最短距離,如果無法從1號點走到n號點,則輸出-1。
輸入格式
第一行包含整數n和m。
接下來m行每行包含三個整數x,y,z,表示存在一條從點x到點y的有向邊,邊長爲z。
輸出格式
輸出一個整數,表示1號點到n號點的最短距離。
如果路徑不存在,則輸出-1。
數據範圍
1≤n≤500,
1≤m≤105,
圖中涉及邊長均不超過10000。
輸入樣例:
3 3
1 2 2
2 3 1
1 3 4
輸出樣例:
3
分析:
具體落實:
模板:
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int N=510;
int n,m,g[N][N],dis[N];
bool st[N];
int dijkstra(int S)
{
memset(dis,0x3f,sizeof dis);
dis[S]=0;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
if(!st[j] && (t==-1 || dis[t]>dis[j]))
t=j;
st[t]=true;
for(int j=1;j<=n;j++)
dis[j]=min(dis[j],dis[t]+g[t][j]);
}
return dis[n]==0x3f3f3f3f ? -1 : dis[n];
}
int main()
{
memset(g,0x3f,sizeof g);
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a][b]=min(g[a][b],c);
}
cout<<dijkstra(1)<<endl;
return 0;
}
1-2、堆優化的Dijkstra算法-O(mlogn)
給定一個n個點m條邊的有向圖,圖中可能存在重邊和自環,所有邊權均爲非負值。
請你求出1號點到n號點的最短距離,如果無法從1號點走到n號點,則輸出-1。
輸入格式
第一行包含整數n和m。
接下來m行每行包含三個整數x,y,z,表示存在一條從點x到點y的有向邊,邊長爲z。
輸出格式
輸出一個整數,表示1號點到n號點的最短距離。
如果路徑不存在,則輸出-1。
數據範圍
1≤n,m≤1.5×105,
圖中涉及邊長均不小於0,且不超過10000。
輸入樣例:
3 3
1 2 2
2 3 1
1 3 4
輸出樣例:
3
分析:
代碼:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
#define P pair<int,int>
using namespace std;
const int N=1e6+10;
int n,m,e[N],w[N],ne[N],h[N],idx;
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
int dijkstra()
{
int dis[N];
memset(dis,0x3f,sizeof dis);
dis[1]=0;
priority_queue<P,vector<P>,greater<P>> heap;
heap.push({0,1});
while(heap.size())
{
P t=heap.top();
heap.pop();
int id=t.second;
if(st[id]) continue;
st[id]=true;
for(int i=h[id];~i;i=ne[i])
{
int j=e[i];
if(dis[j]>dis[id]+w[i])
{
dis[j]=dis[id]+w[i];
heap.push({dis[j],j});
}
}
}
return dis[n]==0x3f3f3f3f ? -1 : dis[n];
}
int main()
{
memset(h,-1,sizeof h);
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
cout<<dijkstra()<<endl;
return 0;
}
2、Bellman-Ford算法(帶負權且經過的邊的數量有限制)-O(nm)
給定一個n個點m條邊的有向圖,圖中可能存在重邊和自環, 邊權可能爲負數。
請你求出從1號點到n號點的最多經過k條邊的最短距離,如果無法從1號點走到n號點,輸出impossible。
注意:圖中可能 存在負權迴路 。
輸入格式
第一行包含三個整數n,m,k。
接下來m行,每行包含三個整數x,y,z,表示存在一條從點x到點y的有向邊,邊長爲z。
輸出格式
輸出一個整數,表示從1號點到n號點的最多經過k條邊的最短距離。
如果不存在滿足條件的路徑,則輸出“impossible”。
數據範圍
1≤n,k≤500,
1≤m≤10000,
任意邊長的絕對值不超過10000。
輸入樣例:
3 3 1
1 2 1
2 3 1
1 3 3
輸出樣例:
3
分析:
代碼:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=10010;
int n,m,k,backup[N];
struct Edge
{
int u,v,w;
}e[N];
int bellman_ford()
{
int dis[N];
memset(dis,0x3f,sizeof dis);
dis[1]=0;
for(int i=0;i<k;i++)
{
memcpy(backup,dis,sizeof dis);
for(int j=1;j<=m;j++)
{
int a=e[j].u,b=e[j].v,w=e[j].w;
dis[b]=min(dis[b],backup[a]+w);
}
}
return dis[n]>0x3f3f3f3f/2 ? -1 : dis[n];
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
int t=bellman_ford();
if(t==-1) puts("impossible");
else cout<<t<<endl;
return 0;
}
3、SPFA算法(帶負權的最短路)-O(m)~O(nm)
3-1、SPFA求最短路
給定一個n個點m條邊的有向圖,圖中可能存在重邊和自環, 邊權可能爲負數。
請你求出1號點到n號點的最短距離,如果無法從1號點走到n號點,則輸出impossible。
數據保證不存在負權迴路。
輸入格式
第一行包含整數n和m。
接下來m行每行包含三個整數x,y,z,表示存在一條從點x到點y的有向邊,邊長爲z。
輸出格式
輸出一個整數,表示1號點到n號點的最短距離。
如果路徑不存在,則輸出”impossible”。
數據範圍
1≤n,m≤105
圖中涉及邊長絕對值均不超過10000。
輸入樣例:
3 3
1 2 5
2 3 -3
1 3 4
輸出樣例:
2
分析:
代碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=100010;
int n,m,e[N],h[N],w[N],ne[N],idx;
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
int spfa()
{
int dis[N];
memset(dis,0x3f,sizeof dis);
dis[1]=0;
queue<int> Q;
Q.push(1);
st[1]=true;
while(Q.size())
{
int u=Q.front();
Q.pop();
st[u]=false;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(dis[j]>dis[u]+w[i])
{
dis[j]=dis[u]+w[i];
if(!st[j])
{
st[j]=true;
Q.push(j);
}
}
}
}
return dis[n];
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
int t=spfa();
if(t==0x3f3f3f3f) puts("impossible");
else cout<<t<<endl;
return 0;
}
3-2、SPFA判斷負環
給定一個n個點m條邊的有向圖,圖中可能存在重邊和自環, 邊權可能爲負數。
請你判斷圖中是否存在負權迴路。
輸入格式
第一行包含整數n和m。
接下來m行每行包含三個整數x,y,z,表示存在一條從點x到點y的有向邊,邊長爲z。
輸出格式
如果圖中存在負權迴路,則輸出“Yes”,否則輸出“No”。
數據範圍
1≤n≤2000,
1≤m≤10000,
圖中涉及邊長絕對值均不超過10000。
輸入樣例:
3 3
1 2 -1
2 3 4
3 1 -4
輸出樣例:
Yes
分析:
代碼:
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2010, M = 10010;
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;
}
4、Floyd(多源最短路)-O(n3)
給定一個n個點m條邊的有向圖,圖中可能存在重邊和自環,邊權可能爲負數。
再給定k個詢問,每個詢問包含兩個整數x和y,表示查詢從點x到點y的最短距離,如果路徑不存在,則輸出“impossible”。
數據保證圖中不存在負權迴路。
輸入格式
第一行包含三個整數n,m,k
接下來m行,每行包含三個整數x,y,z,表示存在一條從點x到點y的有向邊,邊長爲z。
接下來k行,每行包含兩個整數x,y,表示詢問點x到點y的最短距離。
輸出格式
共k行,每行輸出一個整數,表示詢問的結果,若詢問兩點間不存在路徑,則輸出“impossible”。
數據範圍
1≤n≤200,
1≤k≤n2
1≤m≤20000,
圖中涉及邊長絕對值均不超過10000。
輸入樣例:
3 3 2
1 2 1
2 3 2
1 3 1
2 1
1 3
輸出樣例:
impossible
1
代碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int N=210;
int n,m,q,d[N][N];
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
int main()
{
cin>>n>>m>>q;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i==j) d[i][j]=0;
else d[i][j]=inf;
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
d[a][b]=min(d[a][b],c);
}
floyd();
while(q--)
{
int a,b;
scanf("%d%d",&a,&b);
if(d[a][b]>inf/2) puts("impossible");
else printf("%d\n",d[a][b]);
}
return 0;
}