如何在 Boost Dijkstra 中定义自定义距离
How do I define a custom distance in Boost Dijkstra?
我目前正在查看 Boost Dijkstra 的文档 - http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/dijkstra_shortest_paths.html; 我的目标是在计算距离时修改距离组合以获得"最大值"而不是"加号"。文档是这样说的:
IN: distance_combine(CombineFunction cmb)
This function is used to combine distances to compute the distance of a path. The
CombineFunction type must be a model of Binary Function. The first argument typ
of the binary function must match the value type of the DistanceMap property map
and the second argument type must match the value type of the WeightMap property
map. The result type must be the same type as the distance value type.
Default: closed_plus<D> with D=typename property_traits<DistanceMap>::value_type
定义这样一个组合函数的语法是什么?我尝试过摸索 std::max,但我的编译器似乎对它不满意。
我打算采用懒惰的方式,只给出一些代码来展示如何做到这一点:)
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/adjacency_list.hpp>
struct Edge {
Edge(float weight_) : weight(weight_) {}
float weight;
};
// simple function
float combine(float a, float b){
return std::max(a, b);
}
// functor
struct Combine{
// Some internal state
float operator()(float a, float b) const {
return std::max(a, b);
}
};
int main(int, char**){
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::no_property, Edge > graph_t;
typedef boost::graph_traits < graph_t >::vertex_descriptor vertex_t;
graph_t g;
vertex_t a = boost::add_vertex(g);
vertex_t b = boost::add_vertex(g);
vertex_t c = boost::add_vertex(g);
vertex_t d = boost::add_vertex(g);
boost::add_edge(a, b, Edge(3), g);
boost::add_edge(b, c, Edge(3), g);
boost::add_edge(a, d, Edge(1), g);
boost::add_edge(d, c, Edge(4), g);
std::vector<vertex_t> preds(4);
// Traditional dijsktra (sum)
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::weight,g)));
assert(preds[c] == d);
assert(preds[d] == a);
// Dijkstra with custom combine as a function
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::weight,g)).distance_combine(&combine));
assert(preds[c] == b);
assert(preds[b] == a);
// Dijkstra with custom combine as a functior
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::weight,g)).distance_combine(Combine()));
// Dijkstra with custom combine as a lambda
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::weight,g)).distance_combine([](float a, float b){return std::max(a,b);}));
return 0;
}
可能将其参数作为模板可能会使事情变得有点困难......
尝试(其中 T 是距离的类型)
T comb(T& a, T& b) { return std::max(a, b); }
并通过梳子。
相关文章:
- Android Studio:如何在build.gradle中定义自定义宏(针对不同的构建变体),并让原生C / C++
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- C++定义自定义unordered_set的不同方法
- 如何为 std::unordered_set 定义自定义键等效谓词?
- 如何从设备代理定义自定义片段
- 如何定义自定义跨平台size_t类型
- C++ - 定义自定义新建和删除运算符时make_shared
- C++ - 定义自定义析构函数时程序崩溃
- LLDB调试器 - 定义自定义类型显示
- 如何在 Boost Dijkstra 中定义自定义距离
- 为boost哈希映射定义自定义哈希函数
- 在派生类中定义自定义返回类型的受保护方法
- 我可以为我的自定义Qt UI元素定义自定义CSS/QSS属性吗?
- 如何在C++中定义自定义浮点格式(类型)
- 定义自定义扫描运算符
- 在运行时为 std::set 定义自定义比较器
- 如何在 C(++) 中定义自定义错误代码
- 如何为Apache模块定义自定义配置指令
- 声明/定义自定义类定制对象的正确方法
- 为boost中的动态阵列定义自定义步进器