[笔记] 最短路
最短路
性质:
- 对于边权为正的图,任意两个结点之间的最短路,不会经过重复的结点。
- 对于边权为正的图,任意两个结点之间的最短路,不会经过重复的边。
- 对于边权为正的图,任意两个结点之间的最短路,任意一条的结点数不会超过
n ,边数不会超过n-1 。
最短路问题 分为
单源最短路 和 多源最短路 。
前者只需要求 一个固定的起点 到各个顶点的最短路径;
后者则要求得出 任意两个顶点之间 的最短路径。
多源最短路
Floyd 算法
- 特性
适用于任何图,不管有向无向,边权正负,但是最短路必须存在(不能有负环)。
- B3647 代码
#include <bits/stdc++.h>
using namespace std;
#define maxn 10005
#define INF INT_MAX
int dist[maxn][maxn]; // dist[i][j] 表示从 i 到 j 的最短路
int main(){
int n, m; // 点数,边数
cin >> n >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
dist[i][j] = INF;
}
dist[i][i] = 0;
}
for (int i = 1; i <= m; i++){
int u, v, w; // 起点,终点,权值
cin >> u >> v >> w;
dist[u][v] = min(w, dist[u][v]);
dist[v][u] = min(w, dist[v][u]);
}
// 本质上是动态规划思想
for (int k = 1; k <= n; k++){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if(dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] < dist[i][j]){ // 判断是否合法
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
cout << dist[i][j] << " ";
}
cout << endl;
}
return 0;
}
- 复杂度
时间复杂度:
空间复杂度:
单源最短路
Dijkstra 算法
- 特性
适用于求解 非负权图 上单源最短路的算法。
- P4779 代码
#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
#define pii pair<int, int>
vector<pii> edge[maxn];
priority_queue<pii, vector<pii>, greater<pii>> q;
int dist[maxn];
int main(){
int n, m, s;
cin >> n >> m >> s;
memset(dist, -1, sizeof dist);
for (int i = 1; i <= m; i++){
int u, v, w;
cin >> u >> v >> w;
edge[u].push_back({v, w});
}
q.push({0, s});
while(!q.empty()){
auto p = q.top();
q.pop();
if(dist[p.second] >= 0) continue;
dist[p.second] = p.first;
for (auto y : edge[p.second]){
q.push({p.first + y.second, y.first});
}
}
for (int i = 1; i <= n; i++){
cout << dist[i] << " ";
}
return 0;
}
- 复杂度
时间复杂度:
Bellman-Ford 算法
- P3371 代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,m,s;
ll d[10005];
int vis[10005];
vector<pair<int,int>> edge[10005];
int main(){
scanf("%d%d%d",&n,&m,&s);
for (int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
edge[u].push_back({v,w});
}
memset(d,0x3f,sizeof(d));
queue<int> q;
d[s]=0;
q.push(s);
vis[s]=1;
while (!q.empty()){
int x=q.front();
q.pop();
vis[x]=0;
for (auto p:edge[x]){
if (d[x]+p.second<d[p.first]){
d[p.first]=d[x]+p.second;
if (vis[p.first]==0){
q.push(p.first);
vis[p.first]=1;
}
}
}
}
for (int i=1;i<=n;i++){
printf("%lld%c",d[i]<0x3f3f3f3f3f3f3f3fLL?d[i]:INT_MAX,i<n?' ':'\n');
}
return 0;
}
- 复杂度
时间复杂度:
SPFA 算法 ~死了~
- P3371 代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,m,s;
ll d[10005];
int vis[10005];
vector<pair<int,int>> edge[10005];
int main(){
scanf("%d%d%d",&n,&m,&s);
for (int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
edge[u].push_back({v,w});
}
memset(d,0x3f,sizeof(d));
queue<int> q;
d[s]=0;
q.push(s);
vis[s]=1;
while (!q.empty()){
int x=q.front();
q.pop();
vis[x]=0;
for (auto p:edge[x]){
if (d[x]+p.second<d[p.first]){
d[p.first]=d[x]+p.second;
if (vis[p.first]==0){
q.push(p.first);
vis[p.first]=1;
}
}
}
}
for (int i=1;i<=n;i++){
printf("%lld%c",d[i]<0x3f3f3f3f3f3f3f3fLL?d[i]:INT_MAX,i<n?' ':'\n');
}
return 0;
}
- 复杂度
最坏时间复杂度: