我想創建一個圖表并使用不同的權重圖多次呼叫 Dijkstra。我讀到我可以使用 associative_property_map 將邊緣映射到權重,但我不知道如何使用此自定義映射作為權重映射來呼叫 Dijkstra。
#include <iostream>
#include <vector>
#include <map>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
typedef boost::adjacency_list<boost::vecS,
boost::vecS,
boost::undirectedS>
graph;
typedef boost::graph_traits<graph>::edge_descriptor edge_desc;
typedef boost::graph_traits<graph>::vertex_descriptor vertex_desc;
typedef std::map<edge_desc, boost::edge_weight_t> edge_weight_map;
typedef boost::associative_property_map<edge_weight_map> weight_map;
void dijkstra_path(const graph &G, const weight_map &w_map, int s, int t, std::vector<vertex_desc> &pred_map) {
int n = boost::num_vertices(G);
std::vector<int> dist_map(n);
boost::dijkstra_shortest_paths(G, s,
boost::distance_map(
boost::make_iterator_property_map(dist_map.begin(), boost::get(boost::vertex_index, G)))
.predecessor_map(boost::make_iterator_property_map(pred_map.begin(), boost::get(boost::vertex_index, G))),
w_map);
}
這就是我想象的作業方式,有人可以告訴我如何實際做到這一點嗎?
uj5u.com熱心網友回復:
您正在將位置引數與命名引數混合。帶有命名引數的多載不需要權重圖位置:https ://www.boost.org/doc/libs/1_80_0/libs/graph/doc/dijkstra_shortest_paths.html
因此,將權重映射添加到名稱引數中:
using graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
using edge_desc = boost::graph_traits<graph>::edge_descriptor;
using vertex_desc = boost::graph_traits<graph>::vertex_descriptor;
using edge_weight_map = std::map<edge_desc, boost::edge_weight_t>;
using weight_map = boost::associative_property_map<edge_weight_map>;
void dijkstra_path(const graph& G, weight_map w_map, int s, int /*t*/,
std::vector<vertex_desc>& pred_map) //
{
int n = boost::num_vertices(G);
std::vector<int> dist_map(n);
auto id = get(boost::vertex_index, G);
auto dm = make_iterator_property_map(dist_map.begin(), id);
auto pm = make_iterator_property_map(pred_map.begin(), id);
boost::dijkstra_shortest_paths( //
G, s, //
boost::distance_map(dm) //
.predecessor_map(pm) //
.weight_map(w_map));
}
作為一般提示,按值傳遞屬性映射。它們是輕量級的“參考”型別,旨在復制起來非常便宜。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511327.html
標籤:C 促进
上一篇:平均分(柬埔寨偶像練習)
