图的最短路径问题主要包括三种算法:

(1)Dijkstra (没有负权边的单源最短路径)

(2)Floyed (多源最短路径)

(3)Bellman (含有负权边的单源最短路径)

本文主要讲使用C++实现简单的Floyd算法,Floyd算法原理参见 Floyd–Warshall algorithm

Floyd算法简单实现(C++)

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define MAXVEX 10
  5. #define INFINITY 65535
  6.  
  7. typedef int Patharc[MAXVEX][MAXVEX];
  8. typedef int ShortPathTable[MAXVEX][MAXVEX];
  9.  
  10. typedef struct {
  11. int vex[MAXVEX];
  12. int arc[MAXVEX][MAXVEX];
  13. int numVertexes;
  14. } MGraph;
  15.  
  16. // 构建图
  17. void CreateMGraph(MGraph *G){
  18. int i, j, k;
  19.  
  20. // 初始化图
  21. G->numVertexes = ;
  22. for(i = ; i < G->numVertexes; ++i){
  23. G->vex[i] = i;
  24. }
  25. for(i = ; i < G->numVertexes; ++i){
  26. for(j = ; j < G->numVertexes; ++j){
  27. if(i == j)
  28. G->arc[i][j] = ;
  29. else
  30. G->arc[i][j] = G->arc[j][i] = INFINITY;
  31. }
  32. }
  33.  
  34. G->arc[][] = ;
  35. G->arc[][] = ;
  36.  
  37. G->arc[][] = ;
  38. G->arc[][] = ;
  39. G->arc[][] = ;
  40.  
  41. G->arc[][] = ;
  42. G->arc[][] = ;
  43.  
  44. G->arc[][] = ;
  45. G->arc[][] = ;
  46.  
  47. G->arc[][] = ;
  48. G->arc[][] = ;
  49. G->arc[][] = ;
  50.  
  51. G->arc[][] = ;
  52.  
  53. G->arc[][] = ;
  54. G->arc[][] = ;
  55.  
  56. G->arc[][] = ;
  57.  
  58. // 设置对称位置元素值
  59. for(i = ; i < G->numVertexes; ++i){
  60. for(j = i; j < G->numVertexes; ++j){
  61. G->arc[j][i] = G->arc[i][j];
  62. }
  63. }
  64. }
  65.  
  66. // Floyd algorithm
  67. void ShortPath_Floyd(MGraph G, Patharc P, ShortPathTable D){
  68. int i, j, k;
  69. // 二重循环,初始化P, D
  70. for(i = ; i < G.numVertexes; ++i){
  71. for(j = ; j < G.numVertexes; ++j){
  72. D[i][j] = G.arc[i][j];
  73. P[i][j] = j;
  74. }
  75. }
  76. // 三重循环, Floyd algorithm
  77. for(k = ; k < G.numVertexes; ++k){
  78. for(i = ; i < G.numVertexes; ++i){
  79. for(j = ; j < G.numVertexes; ++j){
  80. if(D[i][j] > D[i][k]+D[k][j]){
  81. D[i][j] = D[i][k]+D[k][j];
  82. P[i][j] = P[i][k];
  83. }
  84. }
  85. }
  86. }
  87. }
  88.  
  89. // 打印最短路径
  90. void PrintShortPath(MGraph G, Patharc P, ShortPathTable D){
  91. int i, j, k;
  92. cout<<"各顶点之间的最短路径如下: "<<endl;
  93. for(i = ; i < G.numVertexes; ++i){
  94. for(j = i+; j < G.numVertexes; ++j){
  95. cout<<"v"<<i<<"--"<<"v"<<j<<" "<<"weight: "<<D[i][j]<<" Path: "<<i<<" -> ";
  96. k = P[i][j];
  97. while(k != j){
  98. cout<<k<<" -> ";
  99. k = P[k][j];
  100. }
  101. cout<<j<<endl;
  102. }
  103. cout<<endl;
  104. }
  105. }
  106.  
  107. int main(int argc, char const *argv[]) {
  108. MGraph G;
  109. Patharc P;
  110. ShortPathTable D;
  111. CreateMGraph(&G);
  112. ShortPath_Floyd(G, P, D);
  113. PrintShortPath(G, P, D);
  114. return ;
  115. }

运行结果:

  1. 各顶点之间的最短路径如下:
  2. v0--v1 weight: Path: ->
  3. v0--v2 weight: Path: -> ->
  4. v0--v3 weight: Path: -> -> -> ->
  5. v0--v4 weight: Path: -> -> ->
  6. v0--v5 weight: Path: -> -> -> ->
  7. v0--v6 weight: Path: -> -> -> -> ->
  8. v0--v7 weight: Path: -> -> -> -> -> ->
  9. v0--v8 weight: Path: -> -> -> -> -> -> ->
  10.  
  11. v1--v2 weight: Path: ->
  12. v1--v3 weight: Path: -> -> ->
  13. v1--v4 weight: Path: -> ->
  14. v1--v5 weight: Path: -> -> ->
  15. v1--v6 weight: Path: -> -> -> ->
  16. v1--v7 weight: Path: -> -> -> -> ->
  17. v1--v8 weight: Path: -> -> -> -> -> ->
  18.  
  19. v2--v3 weight: Path: -> ->
  20. v2--v4 weight: Path: ->
  21. v2--v5 weight: Path: -> ->
  22. v2--v6 weight: Path: -> -> ->
  23. v2--v7 weight: Path: -> -> -> ->
  24. v2--v8 weight: Path: -> -> -> -> ->
  25.  
  26. v3--v4 weight: Path: ->
  27. v3--v5 weight: Path: -> ->
  28. v3--v6 weight: Path: ->
  29. v3--v7 weight: Path: -> ->
  30. v3--v8 weight: Path: -> -> ->
  31.  
  32. v4--v5 weight: Path: ->
  33. v4--v6 weight: Path: -> ->
  34. v4--v7 weight: Path: -> -> ->
  35. v4--v8 weight: Path: -> -> -> ->
  36.  
  37. v5--v6 weight: Path: -> ->
  38. v5--v7 weight: Path: ->
  39. v5--v8 weight: Path: -> ->
  40.  
  41. v6--v7 weight: Path: ->
  42. v6--v8 weight: Path: -> ->
  43.  
  44. v7--v8 weight: Path: ->
  45.  
  46. [Finished in .2s]

参考资料:

大话数据结构

Floyd–Warshall algorithm, Wikipedia

Floyd Warshall Algorithm | DP-16 , geeksforgeeks

Floyd算法简单实现(C++)的更多相关文章

  1. 算法笔记_069:Floyd算法简单介绍(Java)

    目录 1 问题描述 2 解决方案 2.1 使用Floyd算法得到最短距离示例 2.2 具体编码   1 问题描述 何为Floyd算法? Floyd算法功能:给定一个加权连通图,求取从每一个顶点到其它所 ...

  2. POJ-2240(floyd算法简单应用)

    Arbitrage poj-2240 #include<iostream> #include<cstdio> #include<cstring> #include& ...

  3. floyd算法 青云的机房组网方案(简单)

    青云的机房组网方案(简单) 青云现在要将 nn 个机房连成一个互相连通的网络.工程师小王设计出一个方案:通过在 nn 个机房之间铺设 n-1n−1 条双向的光纤,将所有的机房连接.可以假设数据在两个机 ...

  4. CCF(通信网络):简单DFS+floyd算法

    通信网络 201709-4 一看到题目分析了题意之后,我就想到用floyd算法来求解每一对顶点的最短路.如果一个点和任意一个点都有最短路(不为INF),那么这就是符合的一个答案.可是因为题目超时,只能 ...

  5. 最短路径之Floyd算法

    Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...

  6. 最短路径问题——floyd算法

    floyd算法和之前讲的bellman算法.dijkstra算法最大的不同在于它所处理的终于不再是单源问题了,floyd可以解决任何点到点之间的最短路径问题,个人觉得floyd是最简单最好用的一种算法 ...

  7. 最短路径——Floyd算法

    如何求一张图中任意两顶点之间的最短路径长度,这里写一种最简单的算法——Floyd算法: #include<stdio.h> #define inf 9999 int main() { ][ ...

  8. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  9. Floyd 算法的动态规划本质

    [转载自:http://www.cnblogs.com/chenying99/p/3932877.html] Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(A ...

随机推荐

  1. YCOJ黑熊过河

    Description 有一只黑熊想过河,但河很宽,黑熊不会游泳,只能借助河面上的石墩跳过去,他可以一次跳一墩,也可以一次跳两墩,但是每起跳一次都会耗费一定的能量,黑熊最终可能因能量不够而掉入水中,所 ...

  2. NSA互联网公开情报收集指南:迷宫中的秘密·下

    猫宁!!! 参考链接: https://www.nsa.gov/news-features/declassified-documents/assets/files/Untangling-the-Web ...

  3. 使用selesium和pytesseract识别验证码,达到登录网页目的

    关于验证码问题,大多可以在网上了解到目前有四种解决方案:1.开发注释验证码2.开发开一个“后门”,设置一个万能码,输入万能码则通过3.通过cookies绕过验证码4.图形识别技术 前三种是比较快速也是 ...

  4. Zookeeper(1、3、5节点)集群安装

    1节点 1 week110的zookeeper的安装 + zookeeper提供少量数据的存储 3节点 hadoop-2.6.0.tar.gz的集群搭建(3节点) hadoop-2.6.0-cdh5. ...

  5. c++继承汇总(单继承、多继承、虚继承、菱形继承)

    多重继承中,一个基类可以在派生层次中出现多次,如果一个派生类有多个直接基类,而这些直接基类又有一个共同的基类,则在最终的派生类中会保留该间接共同基类数据成员的多分同名成员.C++提供虚基类的方法使得在 ...

  6. AtCoder Regular Contest 062 E - AtCoDeerくんと立方体づくり / Building Cubes with AtCoDeer

    题目传送门:https://arc062.contest.atcoder.jp/tasks/arc062_c 题目大意: 给你\(N\)块正方形木板,每块木板四角有四种颜色(可以相同),木板中央有编号 ...

  7. Planning CodeForces - 854C

    Planning CodeForces - 854C 题意:有n架航班,第i架原先的时候是在第i分钟起飞的.现在前k分钟无法有飞机起飞,因此需要调整安排表,延后飞机起飞.仍然要求每一分钟只有一架飞机起 ...

  8. 模拟 HDOJ 5099 Comparison of Android versions

    题目传送门 /* 题意:比较型号的大小 模拟:坑点在长度可能为5,此时设为'A' */ #include <cstdio> #include <algorithm> #incl ...

  9. TNS-12508 When Issuing Any SET Command For The Listene

    TNS-12508 When Issuing Any SET Command For The Listener fact: Oracle Net Services    fact: TNS Liste ...

  10. 转 PHP文件上传$_FILES数组各键值含义说明

    文件上传的html表单: <form enctype="multipart/form-data" action="" method="POST& ...