gpt4 book ai didi

c++ - 给定邻接表有向图,如何仅获得 2 个节点之间的最短路径?

转载 作者:行者123 更新时间:2023-11-28 04:04:19 25 4
gpt4 key购买 nike

<分区>

我有一个用以下代码表示的图表:

typedef adjacency_list < vecS, vecS, directedS, property < vertex_name_t, idType >, property < edge_weight_t, double > > graph_t;

我有两个问题:

  1. 如何在 Boost 中将我的图输入 Dijkstra 算法,我的意思是如何从我的图(它是数千个顶点和边)中提取属性以提供 Dijkstra 参数。

  2. 示例中算法的输出是图中一个节点与其余节点之间的最短路径,那么我如何才能只获得我的源节点和目标节点之间的最短路径?我应该过滤输出结果以构建我的最短路径 vector 吗?

//===========================================================================
MyAlgorithm::MyAlgorithm(graph_t AnyGraph, Vertex VSource){// Parameters Constructor

MyGraph = AnyGraph;
vector<Vertex> p(num_vertices(AnyGraph));
// for(auto j = p.begin(); j != p.end(); ++j)
// cout <<"P["<< *j<<"] = "<<p[*j]<<endl;
vector<double> d(num_vertices(AnyGraph));
// for(auto j2 = d.begin(); j2 != d.end(); ++(j2))
// cout <<"d ["<< *(j2)<<"] = "<<d[*(j2)]<<endl;
//===========================================================================
//Dijkstra_Algorithm
//===========================================================================
cout<<"Before\t"<<endl;
dijkstra_shortest_paths(AnyGraph, VSource,
predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, AnyGraph))).
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, AnyGraph))));
cout<<"After\t"<<endl;
//===========================================================================
}// End of Parameters Constructor

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com