#include #include #include #include #include #include #include #include "shortestpath.h" using namespace boost; bool shortestPath(size_t nodeNum, const std::vector> &edges, const std::vector &weights, size_t start, size_t stop, std::vector *path) { typedef adjacency_list < listS, vecS, undirectedS, no_property, property < edge_weight_t, int > > graph_t; typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; typedef graph_traits < graph_t >::edge_descriptor edge_descriptor; typedef std::pair Edge; size_t edgeNum = edges.size(); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 graph_t g(nodeNum); property_map::type weightmap = get(edge_weight, g); for (std::size_t j = 0; j < edgeNum; ++j) { edge_descriptor e; bool inserted; tie(e, inserted) = add_edge(edges[j].first, edges[j].second, g); weightmap[e] = weights[j]; } #else graph_t g(edges.data(), edges.data() + edgeNum, weights.data(), nodeNum); property_map::type weightmap = get(edge_weight, g); #endif std::vector p(num_vertices(g)); std::vector d(num_vertices(g)); vertex_descriptor s = vertex(start, g); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ has trouble with the named parameters mechanism property_map::type indexmap = get(vertex_index, g); dijkstra_shortest_paths(g, s, &p[0], &d[0], weightmap, indexmap, std::less(), closed_plus(), (std::numeric_limits::max)(), 0, default_dijkstra_visitor()); #else dijkstra_shortest_paths(g, s, predecessor_map(&p[0]).distance_map(&d[0])); #endif auto current = stop; while (current != start) { path->push_back(current); size_t next = p[current]; if (next == current) return false; current = next; } path->push_back(current); return true; }