图论——路径还原

 图的最短距离的路径还原——以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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值