zoukankan      html  css  js  c++  java
  • 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) << "
    ";
      cout << "Edge list:
    ";
      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();
    }





  • 相关阅读:
    Junit单元测试
    win7的6个网络命令
    WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS
    WOJ1022 Competition of Programming 贪心 WOJ1023 Division dp
    woj1019 Curriculum Schedule 输入输出 woj1020 Adjacent Difference 排序
    woj1018(HDU4384)KING KONG 循环群
    woj1016 cherry blossom woj1017 Billiard ball 几何
    woj1013 Barcelet 字符串 woj1014 Doraemon's Flashlight 几何
    woj1012 Thingk and Count DP好题
    woj1010 alternate sum 数学 woj1011 Finding Teamates 数学
  • 原文地址:https://www.cnblogs.com/zx-hit/p/12344683.html
Copyright © 2011-2022 走看看