最短路问题总结
Dijkstra-朴素O(n^2)
- 初始化距离数组, dist[1] = 0, dist[i] = inf;
- for n次循环 每次循环确定一个min加入S集合中,n次之后就得出所有的最短距离
- 不在S中dist_min的点->t
- t->S加入最短路集合
- 用t更新到其他点的距离
Dijkstra-堆优化O(mlogm)
- 利用邻接表,优先队列
- 在priority_queue
,greater > heap;中将返回堆顶 - 利用堆顶来更新其他点,并加入堆中类似宽搜
Bellman_ford O(nm)
- 注意连锁想象需要备份, struct Edge{int a,b,c} Edge[M];
- 初始化dist, 松弛 dist[x.b] = min(dist[x.b], backup[x.a]+x.w);
- 松弛k次,每次访问m条边
Spfa O(n)~O(nm)
- 利用队列优化仅加入修改过的地方
- for k次
- for 所有边利用宽搜模型去优化bellman_ford算法
- 更新队列中当前点的所有出边
Floyd O(n3)
- 初始化d
- k, i, j 去更新d
Dijstra朴素版:
#include#include using namespace std; const int N = 510; int g[N][N], dist[N]; bool st[N]; int n, m; int Dijstra() { memset(dist, 0x3f, sizeof dist); dist[1] = 0; for(int i = 0; i < n; i++) { 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++) { if(dist[j] > dist[t] + g[t][j]) { dist[j] = dist[t] + g[t][j]; } } st[t] = true; } if(dist[n] == 0x3f3f3f3f) return -1; else return dist[n]; } int main() { cin >> n >> m; memset(g, 0x3f, sizeof g); while(m--) { int a, b, c; cin >> a >> b >> c; g[a][b] = min(g[a][b], c); } cout << Dijstra() << endl; return 0; }
Dijstra堆优化版:
#include#include #include using namespace std; const int N = 200010; typedef pair<int,int>PII; int e[N], ne[N], w[N], h[N], idx, dist[N]; bool st[N]; int n, m; void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; } int Dijstra() { memset(dist, 0x3f, sizeof dist); dist[1] = 0; priority_queue , greater > heap; heap.push({0,1}); while(heap.size()) { PII t = heap.top(); heap.pop(); int ver = t.second, distance = t.first; if(st[ver]) continue; st[ver] = true; for(int i = h[ver]; i != -1; i = ne[i]) { int j = e[i]; if(dist[j] > dist[ver] + w[i]) { dist[j] = dist[ver] + w[i]; heap.push({dist[j], j}); } } } if(dist[n] == 0x3f3f3f3f) return -1; else return dist[n]; } int main() { cin >> n >> m; memset(h, -1, sizeof h); while(m--) { int a, b, c; cin >> a >> b >> c; add(a, b, c); } cout << Dijstra() << endl; return 0; }
bellman_Ford:
#include#include using namespace std; const int N = 100010; struct Edge { int a, b, c; }edge[N]; int dist[N], backup[N]; int n, m, k; void bellman_Ford() { memset(dist, 0x3f, sizeof dist); dist[1] = 0; for(int i = 0; i < k; i++) { memcpy(backup, dist, sizeof dist); for(int j = 1; j <= m; j++) { dist[edge[j].b] = min(dist[edge[j].b], backup[edge[j].a] + edge[j].c); } } } int main() { cin >> n >> m >> k; for(int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; edge[i] = {a, b, c}; } bellman_Ford(); if(dist[n] > 0x3f3f3f3f / 2) puts("impossible"); else cout << dist[n] << endl; return 0; }
spfa求最短路:
#include#include #include using namespace std; const int N = 100010; int e[N], ne[N], h[N], w[N], dist[N], idx; bool st[N]; int n, m; void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; } void 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]) { q.push(j); st[j] = true; } } } } } int main() { cin >> n >> m; memset(h, -1, sizeof h); while(m--) { int x, y, z; cin >> x >> y >> z; add(x, y, z); } spfa(); if(dist[n] == 0x3f3f3f3f) puts("impossible"); else cout << dist[n] << endl; return 0; }
spfa判断负环:
#include#include #include using namespace std; const int N = 100010; int e[N], ne[N], h[N], w[N], idx, dist[N], cnt[N]; int n, m; bool st[N]; void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; } bool spfa() { memset(dist, 0x3f, sizeof dist); queue<int>q; for(int i = 1; i <= n; i++) { q.push(i); st[i] = 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]; cnt[j] = cnt[t] + 1; if(cnt[j] >= n) return true; if(!st[j]) { q.push(j); st[j] = true; } } } } return false; } int main() { memset(h, -1, sizeof h); cin >> n >> m; while(m--) { int a, b, c; cin >> a >> b >> c; add(a, b, c); } if(spfa()) puts("Yes"); else puts("No"); return 0; }
Folyd算法:(求多源最短路)
#include#include using namespace std; const int N = 210, INF = 0x3f3f3f3f; int g[N][N]; int n, m, q; void Floyd() { for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) g[i][j] = min(g[i][j], g[i][k] + g[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) g[i][j] = INF; else g[i][j] = 0; while(m--) { int a, b, c; cin >> a >> b >> c; g[a][b] = min(g[a][b], c); } Floyd(); while(q--) { int x, y; cin >> x >> y; if(g[x][y] > INF / 2) puts("impossible"); else cout << g[x][y] << endl; } return 0; }