图的最短距离的路径还原——以dijkstra为例
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAX_V = 1010;
const int INF = 10000000;
int d[MAX_V];
bool use[MAX_V];
int cost[MAX_V][MAX_V];
int V;
int prev[MAX_V];
void dijkstra(int s){
fill(d,d+V,INF);
fill(use,use+V,false);
fill(prev,prev+V,-1);//前驱节点
d[s] = 0;
while(true){
int v=-1;
for(int u=0; u<V; u++){
if(!ues[u]&&(v==-1||d[u]<d[v])) v = u;
}
if(v==-1) break;
use(v) = true;
for(int u=0; u<V; u++){
if(d[u]>d[v]+cost[v][u]){
d[u] = d[v]+cost[v][u];
prev[u]=v;
}
}
}
}
vector<int> get_path(int t){
vector<int> path;
for(; t!=-1; t=prev[t]) path.push_back(t);
reserve(path.begin(),path.end());
return path;
}