2012-12-16 18 views
6

मैं वर्तमान में बूस्ट डिजस्ट्रा - 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 के साथ घूमने की कोशिश की है, लेकिन मेरा कंपाइलर इसके साथ खुश नहीं प्रतीत होता है।

उत्तर

3

शायद होने अपने तर्कों टेम्पलेट्स हो चीज़ें थोड़ी मुश्किल बना सकता है है ...

कोशिश (जहां टी अपने दूरी के प्रकार है)

T comb(T& a, T& b) { return std::max(a, b); } 

और कंघी गुजरती हैं।

+1

देते हैं, यह fumbling का थोड़ा अधिक के साथ काम किया। मैंने 'टेम्पलेट टी कंघी (टी एंड ए, टी एंड बी) {वापसी std :: max (a, b) परिभाषित किया है; } ', और मेरे डिजस्ट्रा में पास किया गया। धन्यवाद! – Balise

4

मैं आलसी तरीका के लिए जा रहा हूँ और बस कुछ कोड से पता चलता है कि कैसे वास्तव में यह करने के लिए :)

#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; 
} 
संबंधित मुद्दे