zoukankan      html  css  js  c++  java
  • 21.boost Ford最短路径算法(效率低)

    到某个节点最近距离                  最短路径当前节点的父节点

    完整代码

     1 #include <iostream>
     2 #include <string>
     3 #include <utility>
     4 #include <vector>
     5 #include <deque>
     6 #include <boost/graph/adjacency_list.hpp>
     7 //A*寻路算法
     8 #include <boostgraphastar_search.hpp>
     9 //dijsktra
    10 #include <boostgraphdijkstra_shortest_paths.hpp>
    11 //bellman-Ford算法
    12 #include <boostgraphellman_ford_shortest_paths.hpp>
    13 using namespace std;
    14 using namespace boost;
    15 
    16 
    17 void main()
    18 {
    19     //定义节点和边的相关对象和属性
    20     enum { u, v, x, y, z, N };
    21     char name[] = { 'u', 'v', 'x', 'y', 'z' };
    22     typedef std::pair < int, int >E;
    23     E edge_array[] = { E(u, y), E(u, x), E(u, v), E(v, u),
    24         E(x, y), E(x, v), E(y, v), E(y, z), E(z, u), E(z, x) };
    25     int weights[] = { -4, 8, 5, -2, 9, -3, 7, 2, 6, 7 };
    26     int num_arcs = sizeof(edge_array) / sizeof(E);
    27 
    28     //定义所用的图种类和相关类型
    29     typedef adjacency_list < vecS, vecS, directedS,
    30         no_property, property < edge_weight_t, int > > Graph;
    31     //生成图对象
    32     Graph g(edge_array, edge_array + num_arcs, weights, N);
    33     graph_traits < Graph >::edge_iterator ei, ei_end;
    34 
    35     //distance用于放置依近到远的路径距离
    36     std::vector<int> distance(N, (std::numeric_limits < short >::max)());
    37     //parent用于放置最短路径生成树的各个顶点的父节点
    38     std::vector<std::size_t> parent(N);
    39     for (int i = 0; i < N; ++i)
    40         parent[i] = i;
    41 
    42 
    43     //将源点z对应距离设为0
    44     distance[z] = 0;
    45     //应用Bellman-Ford算法
    46     bool r = bellman_ford_shortest_paths
    47     (g, int(N), weight_map(get(edge_weight, g)).distance_map(&distance[0]).
    48         predecessor_map(&parent[0]));
    49 
    50     if (r)
    51     {
    52         std::cout << "源点z到各点的最短路径
    ";
    53         std::cout << "目的点	" << "最短路径	" << "最短路径树的父节点:
    ";
    54         for (int i = 0; i < N; ++i)
    55             std::cout << name[i] << ": 	" << distance[i]
    56             << "		" << name[parent[i]] << std::endl;
    57     }
    58     else
    59         std::cout << "negative cycle" << std::endl;
    60     
    61     system("pause");
    62 }
  • 相关阅读:
    数据库性能优化之冗余字段的作用
    SQL里面的排序语句desc和ASC有什么区别
    Mybatis@options注解属性useGeneratedKeys,keyProperty,keyColumn的使用
    关于resultType与parameterType的基本使用和区别
    阿里云Centos7的部署springboot后mysql中文问号乱码
    LINUX下启动/停止/重启MYSQL
    CondenseNet:可学习分组卷积,原作对DenseNet的轻量化改造 | CVPR 2018
    MnasNet:经典轻量级神经网络搜索方法 | CVPR 2019
    MobileNetV1/V2/V3简述 | 轻量级网络
    ShuffleNetV1/V2简述 | 轻量级网络
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8673287.html
Copyright © 2011-2022 走看看