C++ 如何在Boost Dijkstra中定义自定义距离?

C++ 如何在Boost Dijkstra中定义自定义距离?,c++,boost,graph,dijkstra,boost-graph,C++,Boost,Graph,Dijkstra,Boost Graph,我现在正在看Boost Dijkstra的文档-;我的目标是在计算距离时,修改距离组合以获得“max”而不是“plus”。医生说: 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

我现在正在看Boost Dijkstra的文档-;我的目标是在计算距离时,修改距离组合以获得“max”而不是“plus”。医生说:

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
IN:距离联合收割机(联合收割机功能cmb)
此函数用于组合距离以计算路径的距离。这个
CombineFunction类型必须是二进制函数的模型。第一个参数类型
二进制函数的值类型必须与DistanceMap属性映射的值类型匹配
第二个参数类型必须与WeightMap属性的值类型匹配
地图。结果类型必须与距离值类型相同。
默认值:带D=typename属性的closed\u plus\u traits::value\u type

定义这种组合函数的语法是什么?我尝试过在std::max上摸索,但我的编译器似乎对它不满意

将其参数设置为模板可能会使事情变得有点困难

Try(其中T是您的距离类型)


然后通过comb。

我将采用懒惰的方式,只需给出一些代码,说明如何做到这一点:)

#包括
#包括
结构边{
边(浮动权重){权重(权重){}
浮重;
};
//简单函数
浮动联合收割机(浮动a、浮动b){
返回标准::最大值(a,b);
}
//函子
结构联合收割机{
//某些内部状态
浮点运算符()(浮点a,浮点b)常量{
返回标准::最大值(a,b);
}
};
int main(int,char**){
typedef boost::邻接_列表graph\t;
typedef boost::graph_traits::顶点描述符顶点;
图g;
顶点a=boost::添加顶点(g);
顶点_tb=boost::添加顶点(g);
顶点c=boost::添加顶点(g);
顶点d=boost::添加顶点(g);
增加边(a,b,边(3),g);
增加边(b,c,边(3),g);
增加边(a,d,边(1),g);
增加边(d,c,边(4),g);
std::载体preds(4);
//传统dijsktra(总和)
boost::dijkstra_最短路径(g,a,boost::前置_映射(&preds[0])。权重_映射(boost::get(&Edge::weight,g));
断言(preds[c]==d);
断言(preds[d]==a);
//Dijkstra,具有自定义联合收割机功能
boost::dijkstra_最短路径(g,a,boost::前置_映射(&preds[0])。权重_映射(boost::get(&Edge::weight,g))。距离_合并(&combine));
断言(preds[c]==b);
断言(preds[b]==a);
//Dijkstra,具有自定义组合功能
boost::dijkstra_最短路径(g,a,boost::前置_映射(&preds[0])。权重_映射(boost::get(&Edge::weight,g))。距离_组合(combine());
//Dijkstra采用定制的联合收割机作为lambda
boost::dijkstra_最短路径(g,a,boost::前置_映射(&preds[0])。权重_映射(boost::get(&Edge::weight,g))。距离_组合([](float a,float b){return std::max(a,b);});
返回0;
}

事实上,这需要更多的摸索。我定义了
模板T comb(T&a,T&b){return std::max(a,b);}
,并在Dijkstra中传递了comb。谢谢
T comb(T& a, T& b) { return std::max(a, b); }
#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;
}