将一个背景复杂的问题抽象为单源最短路问题。
算法复杂度方面,迪杰斯特拉算法朴素版 O ( n 2 + m ) O(n^2 +m) O(n2+m),堆优化迪杰斯特拉 O ( m l o g n ) O(mlogn) O(mlogn),spfa O ( n m ) / O ( m ) O(nm) / O(m) O(nm)/O(m),floyd O ( n 3 ) O(n^3) O(n3)
朴素dijkstra算法
//双向道路
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2510;
int g[N][N];
int dist[N];
bool st[N];
int n, m, e, s;
void dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
//寻找到s的最短路径
for(int i = 1; i < n; i ++ )//最多扩展n - 1次
{
int t = -1;//在还未确定最短路的点中,寻找距离最小的点
for(int j = 1; j <= n; j ++ )
{
if(!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
}
//用最小点更新其他点
for(int j = 1; j <= n; j ++ )
dist[j] = min(dist[j], dist[t] + g[t][j]);
st[t] = true;//最小点的最短路是确定的
}
}
int main()
{
cin >> n >> m >> s >> e;
memset(g, 0x3f, sizeof g);
for(int i = 1; i <= m; i ++ )
{
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = min(g[a][b], c);
}
dijkstra();
cout << dist[e];
}
堆优化dijkstra
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
const int N = 2510;
const int M = 13000;
typedef pair<int, int> PII;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
bool st[N];
int n, m, s, ed;
void add(int a, int b, int c) // 添加一条边a->b,边权为c
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void dijkstra()
{
memset(st, 0, sizeof st);
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
priority_queue<PII, vector<PII>, greater<PII> > q;
q.push({0, s});
while(q.size())
{
auto t = q.top();
q.pop();
if(st[t.y] || dist[t.y] != t.x) continue;
st[t.y] = true;
for(int i = h[t.y]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t.y] + w[i])
{
dist[j] = dist[t.y] + w[i];
q.push({dist[j], j});
}
}
}
}
int main()
{
cin >> n >> m >> s >> ed;
memset(h, -1, sizeof h);
for(int i = 1; i <= m; i ++ )
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
dijkstra();
cout << dist[ed];
}
bellman-ford算法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 6500;
struct edge
{
int a, b, w;
}edges[2 * N];
int n, m, s, ed;
int dist[N];
void bellman_ford()
{
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
for(int i = 0; i < n; i ++ )
{
for(int j = 1; j <= 2 * m; j ++ )
{
int a = edges[j].a, b = edges[j].b, w = edges[j].w;
if(dist[b] > dist[a] + w)
dist[b] = dist[a] + w;
}
}
}
int main()
{
cin >> n >> m >> s >> ed;
for(int i = 1; i <= m; i ++ )
{
int a, b, w;
cin >> a >> b >> w;
edges[i] = {a, b, w};
edges[i + m] = {b, a, w};
}
bellman_ford();
cout << dist[ed];
}
spfa
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 3000;
const int M = 13000;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
bool st[N];
int n, m, s, ed;
void add(int a, int b, int c) // 添加一条边a->b,边权为c
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
//多次入栈更新,如果没有负环最多会n次入栈
queue<int> q;
q.push(s);
while(q.size())
{
auto 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])
{
q.push(j);
st[j] = true;
}
}
}
}
}
int main()
{
cin >> n >> m >> s >> ed;
memset(h, -1, sizeof h);
for(int i = 1; i <= m; i ++ )
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
spfa();
cout << dist[ed];
}