Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/range.hpp>
- #include <boost/range/irange.hpp>
- #include <boost/graph/adjacency_list.hpp>
- #include <boost/graph/graph_traits.hpp>
- #include <boost/graph/dijkstra_shortest_paths.hpp>
- using namespace std;
- using namespace boost;
- #define N 6
- long s[N] = {0, 5, 7 , 0, 3, 8 }; // source vertices
- long j[N] = {2, 7, 10, 3, 8, 10}; // destination vertices
- long p[N] = {8, 8, 8 , 2, 2, 2 }; // weights
- // One can travel from s[i] to j[i] incurring p[i] cost
- typedef long Weight;
- typedef property<edge_weight_t, Weight> EdgeWeightProperty;
- typedef adjacency_list<listS, vecS, directedS, no_property, EdgeWeightProperty> Graph;
- typedef graph_traits<Graph>::vertex_descriptor Vertex;
- typedef graph_traits<Graph>::vertex_iterator VertexIterator;
- typedef graph_traits<Graph>::edge_descriptor Edge;
- Graph g;
- void makeGraph() {
- for (int i : irange<int>(0, N / 10000)) {
- add_edge(s[i], j[i], s[i], g);
- }
- }
- void findShortestPath() {
- Vertex v0 = vertex(0, g); // source
- Vertex vf = vertex(10, g); // destination
- vector<Vertex> predecessors (num_vertices(g)); // To store parents
- vector<Weight> distances (num_vertices(g)); // To store distances
- // Example 1
- // http://programmingexamples.net/wiki/Boost/BGL/DijkstraComputePath
- typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap;
- IndexMap indexMap = get(vertex_index, g);
- // The following line causes an error
- //typedef property_map<Graph, vertex_name_t>::type NameMap;
- //typedef iterator_property_map<Vertex*, IndexMap, Vertex, Vertex&> PredecessorMap;
- //typedef iterator_property_map<Weight*, IndexMap, Weight, Weight&> DistanceMap;
- //PredecessorMap predecessorMap(&predecessors[0], indexMap);
- //DistanceMap distanceMap(&distances[0], indexMap);
- //dijkstra_shortest_paths(g, v0, distance_map(distanceMap).predecessor_map(predecessorMap));
- // Example 2
- // http://www.boost.org/doc/libs/1_55_0/libs/graph/example/dijkstra-example.cpp
- vector<Vertex> p(num_vertices(g));
- vector<long> d(num_vertices(g));
- auto tmp = predecessor_map(make_iterator_property_map(p.begin(), get(vertex_index, g))).
- distance_map(make_iterator_property_map(d.begin(), get(vertex_index, g)));
- // The following line causes an error
- dijkstra_shortest_paths(g, s, tmp);
- }
- int main() {
- makeGraph();
- findShortestPath();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement