1.朴素Dijkstra算法
适合稠密图(以边的多少来判断稠密与否),边长(m)与点数(n)的平方(n2)等价,时间复杂度为(N^2);
代码实现
单看板子:解析
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
int n,m,g[N][N],dist[N];
bool st[N];
int dijkstra()
{
//起始点就是0,然后其他的点就是无穷大,便于后续比较
memset(g, 0x3f3f, sizeof(g));
dist[1]=0;
for(int i = 1; i<=n;i ++)
{
//找到当前未确定最短路的点中,距离最短的点
int t = -1;
for(int j = 1; j <= n ;j ++)
{
if(!st[j] && (t == -1 || dist[t] > dist [j])) t = j;
}
//用t更新其他的点
for(int j = 1; j <= n;j ++)
{
dist[j]=min(dist[j],dist[t]+g[t][j]);
}
//t已确定最短路
st[t] = true;
}
return dist[n] != INF ? dist[n] : -1;
}
AC代码:
#include <iostream>
using namespace std;
const int N = 510;
int n,m,g[N][N];
int dist[N];
bool st[N];
int dijkstra()
{
memset(dist, 0x3f, sizeof(dist));
for( int i=1;i<=n;i++){
int t = -1;
for(int j=1;j<=n;j++)
//不在s中的距离最近的点
if(!st[j]&&(t==-1||dist[t]>dist[j]))
t = j;
//走过了,就标记一下
st[t] = true;
//用t更新其他点的距离
for(int j = 1; j <= n; j++){
dist[j] = min(dist[j],dist[t] + g[t][j]);
}
}
if(dist[n] == 0x3f3f3f) return -1;
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);
}
int t = dijkstra();
cout<<t<<endl;
return 0;
}
2.堆优化的最短路算法
适用于稀疏图,时间复杂度为(mlogn)
#include <iostream>
#include <string.h>
#include <cstring>
#include <math.h>
#include <queue>
#define int long long
#define PII pair<int,int>
using namespace std;
const int N = 1e6+10;
const int INF = 0x3f3f3f3f3f;
int n,m,dist[N];
int h[N],ne[N],e[N],w[N],idx;//用临接表来存稀疏图;
bool st[N];
int dijkstra()
{
memset(dist, 0x3f, sizeof(dist));
dist[1]=0;
/*
PII:first存dist,second存点的编号
小根堆维护当前未入st的dist最小值
*/
priority_queue<PII,vector<PII>,greater<PII>>heap;
heap.push({0,1});
while(heap.size())
{
//t其实有两个值可以存下来
auto t = heap.top();
heap.pop();
int ver = t.second, d = 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});
}
}
}
return dist[n] != INF ? dist[n] : -1;
}
3.贝尔曼-ford算法:
适用于有负权边,单源汇,稀疏图,最短路:
const int M = 1e5+10;
const int INF = 0x3f3f3f3f;
int n,m,k,dist[N],backup[N];
//存边
struct Edge {
int x,y,w;
}edge[M];
void bellman_ford()
{
//初始化
memset(dist , 0x3f, sizeof(dist));
dist[0]=1;
//最多不超过k条边的最短路
for(int i = 1;i <= k;i ++ )
{
memcpy(backup, dist , sizeof(dist));
for(int j = 1;j <= m; j ++ )
{
auto e = edge[j];
dist[e.y] = min(dist[e.y],dist[e.x]+e.w);
}
}
//因为此处可能会更新为INF-n(小)
if(dist[n]> INF /2 ) puts("impossible");
else printf("%d\n",dist[n]);
}
4.SPFA(负权边,ford优化)单源汇,最短路
:
const int N = 510;
const int M = 1e5+10;
const int INF = 0x3f3f3f3f;
int n,m,k;
int h[N],e[M],w[M],ne[M],idx;//临接表存图
int dist[N],cnt[N];//cnt数组记录到点i的最短路经过多少条边
bool st[N];
//存边
void spfa()
{
queue<int> q;
memset(dist, 0x3f, sizeof(dist));
dist[1]=0;q.push(1); st[1]=true;
while(q.size())
{
int t = q.front();
q.pop(),st[t] = false;
for( int i = h[i];i!=-1;i=ne[i])
{
int j = e[i];
if(dist[j]>dist[i]+w[i])
{
dist[j] = dist[i] + w[i];
if(!st[j]) q.push(j),st[j]=true;
}
}
}
if(dist[n] == INF) puts("impossible"); // 无通路
else printf("%d\n", dist[n]);
}
查看是否闭环:
const int N = 510;
const int M = 1e5+10;
const int INF = 0x3f3f3f3f;
int n,m,k;
int h[N],e[M],w[M],ne[M],idx;//临接表存图
int dist[N],cnt[N];//cnt数组记录到点i的最短路经过多少条边
bool st[N];
//存边
//int cnt[N];
bool spfa()
{
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[i];i!=-1;i=ne[i])
{
int j = e[i];
if(dist[j]>dist[i]+w[i])
{
dist[j] = dist[i] + w[i];
cnt[j] = cnt[i] + 1;
if(cnt[j]>=n) return true;
if(!st[j]) q.push(j),st[j]=true;
}
}
}
return false;
}
Floyd算法,多源汇,最短路。
/*
AC是算法核心
Floyd算法,多源汇,最短路。
*/
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 210, INF = 1e9;
int n,m,Q;
int d[N][N];//用临接矩阵存图
void AC()
{
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;
}
//然后构建关系
while(m--)
{
int a,b,w;
cin>>a>>b>>w;
d[a][b]=min(d[a][b],w);
}
//Floyd算法求最短路
AC();
//查询
//判断是否有闭环
while(Q--)
{
int a,b;
cin>>a>>b;
if(d[a][b] > INF / 2) puts("impossible");
else cout<<d[a][b]<<endl;
}
return 0;
}