BOOST DIJKSTRA unit's most path example

xiaoxiao2021-03-06  19

// Solving by Robinkin from Devonit # include #include #include

#include #include #include

USING NAMESPACE BOOST;

intmain (int, char * []) ​​{typedef adjacency_list > graph_t; typedef graph_traits :: vertex_descriptor vertex_descriptor; typedef graph_traits :: edge_descriptor edge_descriptor; Typedef std :: pair Edge;

Const int num_nodes = 5; enum nodes {a, b, c, d, e}; char name [] = "abcde"; Edge Edge_Array [] = {Edge (A, C), Edge (B, B), Edge (B, D), EDGE (B, E), EDGE (C, B), EDGE (C, D), EDGE (E), EDGE (E, A), Edge (E, B)}; int Weights [] = {1, 2, 1, 2, 7, 3, 1, 1, 1};

int num_arcs = sizeof (edge_array) / sizeof (Edge); graph_t g (edge_array, edge_array num_arcs, weights, num_nodes); property_map :: type weightmap = get (edge_weight, g); std :: vector < Vertex_Descriptor> P (Num_vertices (g)); // Front Cards Std :: Vector D (Num_vertices (g)); // Distance Vertex_Descriptor S = Vertex (A, G); // Source Point is A / / Start the shortest path from A

Dijkstra_shortest_paths (g, s, predecessor_map (& P [0]). Distance_map (& D [0]));

Std :: cout << "<< std :: endl; graph_traits :: vertex_iterator vi, vend; for (tie (vi, vend) = vertices (g); vi! = vend; vi) {std :: cout << "distance (" << name [* vi] << ") =" << d [* vi] << ","; std :: cout << "Parent (" << Name [* vi] << ") =" << Name [p [p [p [* vi]] << std :: end1;} returnif;

// Output: distances and piers: distance (a) = 0, Parent (a) = adistance (b) = 6, Parent (b) = edistance (c) = 1, Parent (c) = adistance (d) = 4 , Parent (d) = cdistance (e) = 5, Parent (e) = d

转载请注明原文地址:https://www.9cbs.com/read-53585.html

New Post(0)