直接贴代码吧,简明易懂。

后面自己写了测试,输入数据为:

  1. a
  2. b
  3. c
  4. d
  5. e
  6. 0 1 4
  7. 0 2 2
  8. 1 2 3
  9. 1 3 2
  10. 1 4 3
  11. 2 1 1
  12. 2 3 4
  13. 2 4 5
  14. 4 3 1

也就是课本上111的图4.9(上面为原图,下面为结果)

程序的输出结果为:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. const int maxVertexNum = 20;
  6. const int INF = 99999;
  7.  
  8. typedef struct dGraph{
  9. // vertexes
  10. string vertex[maxVertexNum];
  11. // edges
  12. int edges[maxVertexNum][maxVertexNum];
  13. int vertexNum;
  14. int edgeNum;
  15. // construct a graph
  16. void set(int n, int e) {
  17. vertexNum = n;
  18. edgeNum = e;
  19. //cout << "input vertex" << endl;
  20. for (int i = 0; i < n; i++)
  21. cin >> vertex[i];
  22. for (int i = 0; i < n; i++)
  23. for (int j = 0; j < n ; j++)
  24. edges[i][j] = INF;
  25. //cout << "input edge" << endl;
  26. int weight;
  27. for (int m = 0; m < e; m++) {
  28. int i,j;
  29. cin >> i >> j >> weight;
  30. edges[i][j] = weight;
  31. }
  32. }
  33. // Dijkstra's shortest-path alogorithm
  34. void shortestPathDj(int v) {
  35. bool visited[vertexNum] = {false};
  36. int dist[vertexNum] = {INF};
  37. string path[2 * vertexNum];
  38.  
  39. // initiation
  40. for (int i = 0; i < vertexNum; i++) {
  41. dist[i] = edges[v][i];
  42. if (dist[i] < INF)
  43. path[i] = vertex[v]+vertex[i];
  44. else
  45. path[i] = "";
  46. }
  47. dist[v] = 0;
  48. visited[v] = 1;
  49. //
  50. int min;
  51. int i, j, k;
  52. for (j = 1; j < vertexNum; j++) {
  53. min = INF;
  54. // find shortest edge.
  55. for (i = 0; i < vertexNum; i++) {
  56. if (dist[i] < min && visited[i] == false) {
  57. min = dist[i];
  58. k = i;
  59. }
  60. }
  61. visited[k] = true;
  62. cout<<path[k]<<" "<<dist[k]<<endl;
  63. for (i = 0; i < vertexNum; i++) {
  64. if (dist[i] > dist[k] + edges[k][i] && visited[i] == false) {
  65. dist[i] = dist[k] + edges[k][i];
  66. path[i] = path[k] + vertex[i];
  67. }
  68. }
  69. }
  70. }
  71.  
  72. } dGraph;
  73.  
  74. int main() {
  75. freopen("in.txt", "r", stdin);
  76. dGraph G;
  77. G.set(5,9);
  78. G.shortestPathDj(0);
  79. return 0;
  80. }

  

graph-Dijkstra's shortest-path alogorithm的更多相关文章

  1. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  2. HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...

  3. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  4. HDU 4725 The Shortest Path in Nya Graph(构图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. The Shortest Path in Nya Graph HDU - 4725

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  7. HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  8. HDU4725:The Shortest Path in Nya Graph(最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  10. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

随机推荐

  1. 如何使用在Windows 下AspNetCore Api 和 consul

    在Windows 下如何使用 AspNetCore Api 和 consul https://blog.csdn.net/sD7O95O/article/details/80750803 一.概念:什 ...

  2. 简单记录下HTTPS中的SSL

    大概思路 大概思路是混合加密的方式,即对称加密方式混合非对称加密方式. 非对称加密会更加安全,功能也更强大,但他复杂而且速度慢. 对称加密速度快,但要保证这个公共密钥的正确性和真实性. 所以两者结合, ...

  3. 042 Trapping Rain Water 接雨水

    给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...

  4. Django 的一些错误以及处理

    django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 589: 'static', expected 'e ...

  5. python实现批量远程执行命令及批量上传下载文件

    #!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...

  6. JAVA基础之项目分包

    个人理解: 项目分层分包适合多人开发合作的,最好一个界面设置一个view,同时注释一定设置好,按照顺序:从前向后进行传递参数,从后向前进行传递返回值来进行判断是否真正的执行了sql语句(可以不返回), ...

  7. 使用js获取复选框的值,并把数组传回后台处理,过程使用的是Ajax异步查询

    这是界面代码: ​ function shua(){             var id_array=new Array();         $('input[id="checkAll& ...

  8. Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.

    Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.七宗罪:暴食.贪婪.懒惰.暴怒.傲慢.色欲.妒忌.

  9. Beta_版本发布

    学号 姓名 201731041215 王阳 201731062302 鲜雨珂 201731062128 邓捷 201731062305 周蓉 201731062131 龙继平 201731062304 ...

  10. 【虚拟机-虚拟网络】使用 PsPing & PaPing 进行 TCP 端口连通性测试

    PsPing & PaPing 介绍 通常,我们测试数据包能否通过 IP 协议到达特定主机时,都习惯使用 ping 命令.工作时 ping 向目标主机发送一个 IMCP Echo 请求的数据包 ...