单源最短路问题:
单源问题又有含负边问题
正权图:
朴素dijksra o(n2) 与边无关 适合稠密图
堆优化版o(mlogn) 适合稀疏图
负权边:
BF-o(nm)
SPFA-o(m) 最坏o(nm) 但并不是所有都能用
多源汇最短路问题:
floyed o(n3)

dijkstra算法:
重点在于把第一个点加入的时候,把相邻的点更新了,不管有用没用,直接用最大值干!
#include <iostream>
#include <cstring>
using namespace std;
const int N = 505;
int g[N][N];
bool st[N];
int dist[N];
int n,m;
void dijkstra(){
memset(dist,0x3f,sizeof dist);
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;
}
st[t] = true;
for(int j = 1;j<=n;j++){
if(!st[j])
dist[j]=min(g[t][j]+dist[t],dist[j]);
}
}
if(dist[n]==0x3f3f3f3f){
cout<<-1;
}
else
cout<<dist[n];
}
int main() {
cin>>n>>m;
memset(g,0x3f,sizeof g);
for(int i = 0;i<m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
// cin>>a>>b>>c;
g[a][b] = min(g[a][b],c);
}
dijkstra();
return 0;
}

1万+




被折叠的 条评论
为什么被折叠?



