• <legend id='hTkbv'><style id='hTkbv'><dir id='hTkbv'><q id='hTkbv'></q></dir></style></legend>
  • <tfoot id='hTkbv'></tfoot>

    <small id='hTkbv'></small><noframes id='hTkbv'>

  • <i id='hTkbv'><tr id='hTkbv'><dt id='hTkbv'><q id='hTkbv'><span id='hTkbv'><b id='hTkbv'><form id='hTkbv'><ins id='hTkbv'></ins><ul id='hTkbv'></ul><sub id='hTkbv'></sub></form><legend id='hTkbv'></legend><bdo id='hTkbv'><pre id='hTkbv'><center id='hTkbv'></center></pre></bdo></b><th id='hTkbv'></th></span></q></dt></tr></i><div id='hTkbv'><tfoot id='hTkbv'></tfoot><dl id='hTkbv'><fieldset id='hTkbv'></fieldset></dl></div>
    • <bdo id='hTkbv'></bdo><ul id='hTkbv'></ul>

        权重图作为 Boost Graph Dijkstra 算法中的函数

        时间:2023-07-19
        <tfoot id='ASgzu'></tfoot>
            <bdo id='ASgzu'></bdo><ul id='ASgzu'></ul>

              <small id='ASgzu'></small><noframes id='ASgzu'>

            1. <i id='ASgzu'><tr id='ASgzu'><dt id='ASgzu'><q id='ASgzu'><span id='ASgzu'><b id='ASgzu'><form id='ASgzu'><ins id='ASgzu'></ins><ul id='ASgzu'></ul><sub id='ASgzu'></sub></form><legend id='ASgzu'></legend><bdo id='ASgzu'><pre id='ASgzu'><center id='ASgzu'></center></pre></bdo></b><th id='ASgzu'></th></span></q></dt></tr></i><div id='ASgzu'><tfoot id='ASgzu'></tfoot><dl id='ASgzu'><fieldset id='ASgzu'></fieldset></dl></div>
              <legend id='ASgzu'><style id='ASgzu'><dir id='ASgzu'><q id='ASgzu'></q></dir></style></legend>
                  <tbody id='ASgzu'></tbody>
                  本文介绍了权重图作为 Boost Graph Dijkstra 算法中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Boost Graph Libraries 并且需要使用一个权重图,它不是常数,但它是参数 K 的函数(即边缘成本取决于 K).在实践中,给定以下代码:

                  I'm using Boost Graph Libraries and need to use a weightmap which is not constant, but which is a function of a parameter K (i.e. the edge costs depend on K). In practice, given the following code:

                  #include <boost/config.hpp>
                  #include <iostream>
                  #include <fstream>
                  #include <boost/graph/graph_traits.hpp>
                  #include <boost/graph/dijkstra_shortest_paths.hpp>
                  #include <boost/graph/adjacency_list.hpp>
                  
                  struct Edge {
                          Edge(float weight_) : weight(weight_) {}
                          float weight;
                          float getWeight(int K)
                          {
                              return K*weight;
                          }
                  };
                  
                  
                  
                  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)));
                  
                          return 0;
                  }
                  

                  我想调用 Dijkstra 算法如下:

                  I'd like to call Dijkstra algorithm as follows:

                  boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::getWeight(2),g)));
                  

                  但错误如下

                  不能在没有的情况下调用成员函数‘float Edge::getWeight(int)’对象

                  cannot call member function ‘float Edge::getWeight(int)’ without object

                  有人知道如何解决这个问题吗?

                  Does anyone know how to solve this?

                  推荐答案

                  属性映射有多种风格.特别是这里可以使用transform_value_property_map.

                  There are a number of property map flavours. In particular one is the transform_value_property_map can be used here.

                  假设你会写 c++03:

                  Assuming c++03 you'd write:

                  生活在 Coliru

                  #include <boost/property_map/transform_value_property_map.hpp>
                  #include <boost/bind.hpp>
                  
                  // ...
                  
                  boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(
                              boost::make_transform_value_property_map(
                                  boost::bind(&Edge::getWeight ,_1, 2), 
                                  boost::get(boost::edge_bundle, g))
                          ));
                  

                  更清洁的 C++11

                  生活在 Coliru

                  auto wmap = make_transform_value_property_map([](Edge& e) { return e.getWeight(2); }, get(boost::edge_bundle, g));
                  boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(wmap));
                  

                  您可以删除 boost/bind.hpp 包含.

                  生活在 Coliru

                  你实际上并不需要它.您可以就地编写 Phoenix 演员:

                  You don't actually need it. You could write a Phoenix actor in-place:

                  #include <boost/phoenix.hpp>
                  using boost::phoenix::arg_names::arg1;
                  
                  auto wmap = make_transform_value_property_map(2 * (&arg1->*&Edge::weight), get(boost::edge_bundle, g));
                  

                  或者再次使用c++11:

                  Or use c++11 again:

                  生活在 Coliru

                  auto wmap = make_transform_value_property_map([](Edge& e) { return e.weight * 2; }, get(boost::edge_bundle, g));
                  

                  这篇关于权重图作为 Boost Graph Dijkstra 算法中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Boost ptree 将 JSON 数组解析为 std::string 下一篇:Boost group_threads 最大并行线程数

                  相关文章

                    <small id='lPgQj'></small><noframes id='lPgQj'>

                      <bdo id='lPgQj'></bdo><ul id='lPgQj'></ul>
                  1. <i id='lPgQj'><tr id='lPgQj'><dt id='lPgQj'><q id='lPgQj'><span id='lPgQj'><b id='lPgQj'><form id='lPgQj'><ins id='lPgQj'></ins><ul id='lPgQj'></ul><sub id='lPgQj'></sub></form><legend id='lPgQj'></legend><bdo id='lPgQj'><pre id='lPgQj'><center id='lPgQj'></center></pre></bdo></b><th id='lPgQj'></th></span></q></dt></tr></i><div id='lPgQj'><tfoot id='lPgQj'></tfoot><dl id='lPgQj'><fieldset id='lPgQj'></fieldset></dl></div>
                      <tfoot id='lPgQj'></tfoot>
                      <legend id='lPgQj'><style id='lPgQj'><dir id='lPgQj'><q id='lPgQj'></q></dir></style></legend>