Advertisement
gladiusmaximus

Untitled

May 23rd, 2014
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <boost/range.hpp>
  2. #include <boost/range/irange.hpp>
  3. #include <boost/graph/adjacency_list.hpp>
  4. #include <boost/graph/graph_traits.hpp>
  5. #include <boost/graph/dijkstra_shortest_paths.hpp>
  6. using namespace std;
  7. using namespace boost;
  8.  
  9. #define N 6
  10. long s[N] = {0, 5, 7 , 0, 3, 8 }; // source vertices
  11. long j[N] = {2, 7, 10, 3, 8, 10}; // destination vertices
  12. long p[N] = {8, 8, 8 , 2, 2, 2 }; // weights
  13. // One can travel from s[i] to j[i] incurring p[i] cost
  14.  
  15. typedef long Weight;
  16. typedef property<edge_weight_t, Weight> EdgeWeightProperty;
  17. typedef adjacency_list<listS, vecS, directedS, no_property, EdgeWeightProperty> Graph;
  18. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  19. typedef graph_traits<Graph>::vertex_iterator VertexIterator;
  20. typedef graph_traits<Graph>::edge_descriptor Edge;
  21.  
  22. Graph g;
  23.  
  24. void makeGraph() {
  25.   for (int i : irange<int>(0, N / 10000)) {
  26.     add_edge(s[i], j[i], s[i], g);
  27.   }
  28. }
  29.  
  30.  
  31. void findShortestPath() {
  32.   Vertex v0 = vertex(0, g); // source
  33.   Vertex vf = vertex(10, g);  // destination
  34.   vector<Vertex> predecessors (num_vertices(g)); // To store parents
  35.   vector<Weight> distances (num_vertices(g)); // To store distances
  36.  
  37.   // Example 1
  38.   // http://programmingexamples.net/wiki/Boost/BGL/DijkstraComputePath
  39.  
  40.   typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap;
  41.   IndexMap indexMap = get(vertex_index, g);
  42.  
  43.   // The following line causes an error
  44.   //typedef property_map<Graph, vertex_name_t>::type NameMap;
  45.   //typedef iterator_property_map<Vertex*, IndexMap, Vertex, Vertex&> PredecessorMap;
  46.   //typedef iterator_property_map<Weight*, IndexMap, Weight, Weight&> DistanceMap;
  47.  
  48.   //PredecessorMap predecessorMap(&predecessors[0], indexMap);
  49.   //DistanceMap distanceMap(&distances[0], indexMap);
  50.  
  51.   //dijkstra_shortest_paths(g, v0, distance_map(distanceMap).predecessor_map(predecessorMap));
  52.  
  53.   // Example 2
  54.   // http://www.boost.org/doc/libs/1_55_0/libs/graph/example/dijkstra-example.cpp
  55.  
  56.   vector<Vertex> p(num_vertices(g));
  57.   vector<long> d(num_vertices(g));
  58.  
  59.   auto tmp = predecessor_map(make_iterator_property_map(p.begin(), get(vertex_index, g))).
  60.     distance_map(make_iterator_property_map(d.begin(), get(vertex_index, g)));
  61.  
  62.   // The following line causes an error
  63.   dijkstra_shortest_paths(g, s, tmp);
  64. }
  65.  
  66. int main() {
  67.   makeGraph();
  68.   findShortestPath();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement