C++Boost实现Dijkstra算法搜索最小路径

参考:https://jingyan.baidu.com/article/6c67b1d65db36d2786bb1e67.html

用Dijkstra算法找出该图中从a点到达所有其它结点的最短路径,并给处路径信息

用boost的图论库编辑代码如下:

#include <QCoreApplication>
#include <iostream>
#include <string>
#include <cstdint>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/edge_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/bellman_ford_shortest_paths.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/iteration_macros.hpp>

using namespace boost;

using namespace std;
int main(int argc, char* argv[])
{
  QCoreApplication App(argc, argv);
  typedef property<edge_weight_t, int> EdgeWeightProperty;
  typedef adjacency_list<listS, vecS, directedS,no_property, EdgeWeightProperty> DirectedGraph;
  typedef graph_traits<DirectedGraph>::edge_iterator EdgeIterator;
  typedef property_map <DirectedGraph, vertex_index_t >::type IndexMap;
  typedef iterator_property_map<char *, IndexMap, char, char & > PredecessorMap;
  typedef iterator_property_map<int *, IndexMap, int, int & > DistanceMap;
  typedef graph_traits<DirectedGraph>::vertex_descriptor VertexDescriptor;

  enum MyNode{ a, b, c, d, e, f, h, k };
  char names[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'K' };
  const int num_nodes = sizeof(names)/sizeof(char);
  cout<<"Num of nodes:"<<num_nodes<<endl;
  typedef std::pair<int, int> P;
  P edges[] = {   // 路线
    P(a, b), P(a, c), P(a, d), P(b, h),
    P(c, d), P(c, e), P(c, h),
    P(d, e), P(d, k),
    P(e, f), P(e, k),
    P(f, h), P(f, k),
    P(h, e), P(h, k),
  };

  int weights[] = {
    9, 14, 15, 24,
    5, 30, 18,
    20, 44,
    11, 16,
    6, 6,
    2, 19
  };

  DirectedGraph g(edges, edges + sizeof(edges) / sizeof(P), weights, num_nodes);
  property_map<DirectedGraph, edge_weight_t>::type edgeWeightMap = get(edge_weight_t(), g);
  pair<EdgeIterator, EdgeIterator> ei = boost::edges(g);
  cout << "Number of edges = " << num_edges(g) << "\n";
  cout << "Edge list:\n";
  for (EdgeIterator it = ei.first; it != ei.second; ++it )
  {
    char source = names[boost::source(*it, g)];
    char target = names[boost::target(*it, g)];
    cout <<source <<"->"<<target<< " "<<edgeWeightMap[*it]<< endl;
  }
  cout << endl;

  // Create things for Dijkstra
  vector<char> predecessors(boost::num_vertices(g));  // To store parents
  vector<int> distances(boost::num_vertices(g));      // To store distances
  IndexMap indexMap = boost::get(boost::vertex_index, g);
  PredecessorMap predecessorMap(&predecessors[0], indexMap);
  DistanceMap distanceMap(&distances[0], indexMap);
  VertexDescriptor start = vertex(a, g);
  dijkstra_shortest_paths( g, start, distance_map(distanceMap).predecessor_map(predecessorMap));
  int goalIdx = a;
  BGL_FORALL_VERTICES(goalIdx, g, DirectedGraph)
  {
    VertexDescriptor goal = vertex(goalIdx, g);
    cout << "From " << names[a] << " to " << names[goalIdx]
         <<", min distance:" << distanceMap[goalIdx] << ", ";
    vector<VertexDescriptor> path;
    VertexDescriptor current = goal;
    while(current!=start)
    {
      path.push_back(current);
      current = predecessors[current];
    }
    path.push_back(start);
    // Prints the path obtained in reverse
    for(vector<VertexDescriptor>::reverse_iterator ri = path.rbegin(); ri != path.rend(); ++ri)
    {
      if(ri!=path.rbegin())
        cout << "->";
      cout << names[*ri];
    }
    cout << endl;
  }
  return App.exec();
}

猜你喜欢

转载自www.cnblogs.com/zx-hit/p/12344683.html
今日推荐