//最短路的路径还原
//dijstra
#include<iostream>
#include<vector>
using namespace std;
const int maxn=100010;
const int INF=1e6;
int cost[maxn][maxn];
int V;
int pre[maxn];
bool used[maxn];
int d[maxn];
void dijkstra(int s){
fill(d,d+V,INF);
fill(used,used+V,false);
fill(pre,pre+V,-1);
d[s]=0;
while(true){
int v=-1;
for(int u=0;u<V;u++){
if((d[u]<d[v]||v==-1)&&!used[u]) v=u;
}
if(v==-1) break;
used[v]=true;
for(int u=0;u<V;u++){
if(d[u]>d[v]+cost[v][u]){
d[u]=d[v]+cost[v][u];
pre[u]=v;
}
}
}
}
vector<int> get_path(int t){
vector<int>path;
for(;t!=-1;t=pre[t]) path.push_back(t);
reverse(path.begin(), path.end());
return path;
}
算法入门 18.最短路的路径还原(Dijkstra)
最新推荐文章于 2022-01-02 17:52:51 发布