graph-Dijkstra's shortest-path alogorithm
直接贴代码吧,简明易懂。
后面自己写了测试,输入数据为:
- a
- b
- c
- d
- e
- 0 1 4
- 0 2 2
- 1 2 3
- 1 3 2
- 1 4 3
- 2 1 1
- 2 3 4
- 2 4 5
- 4 3 1
也就是课本上111的图4.9(上面为原图,下面为结果)
程序的输出结果为:
- #include <iostream>
- #include <string>
- using namespace std;
- const int maxVertexNum = 20;
- const int INF = 99999;
- typedef struct dGraph{
- // vertexes
- string vertex[maxVertexNum];
- // edges
- int edges[maxVertexNum][maxVertexNum];
- int vertexNum;
- int edgeNum;
- // construct a graph
- void set(int n, int e) {
- vertexNum = n;
- edgeNum = e;
- //cout << "input vertex" << endl;
- for (int i = 0; i < n; i++)
- cin >> vertex[i];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n ; j++)
- edges[i][j] = INF;
- //cout << "input edge" << endl;
- int weight;
- for (int m = 0; m < e; m++) {
- int i,j;
- cin >> i >> j >> weight;
- edges[i][j] = weight;
- }
- }
- // Dijkstra's shortest-path alogorithm
- void shortestPathDj(int v) {
- bool visited[vertexNum] = {false};
- int dist[vertexNum] = {INF};
- string path[2 * vertexNum];
- // initiation
- for (int i = 0; i < vertexNum; i++) {
- dist[i] = edges[v][i];
- if (dist[i] < INF)
- path[i] = vertex[v]+vertex[i];
- else
- path[i] = "";
- }
- dist[v] = 0;
- visited[v] = 1;
- //
- int min;
- int i, j, k;
- for (j = 1; j < vertexNum; j++) {
- min = INF;
- // find shortest edge.
- for (i = 0; i < vertexNum; i++) {
- if (dist[i] < min && visited[i] == false) {
- min = dist[i];
- k = i;
- }
- }
- visited[k] = true;
- cout<<path[k]<<" "<<dist[k]<<endl;
- for (i = 0; i < vertexNum; i++) {
- if (dist[i] > dist[k] + edges[k][i] && visited[i] == false) {
- dist[i] = dist[k] + edges[k][i];
- path[i] = path[k] + vertex[i];
- }
- }
- }
- }
- } dGraph;
- int main() {
- freopen("in.txt", "r", stdin);
- dGraph G;
- G.set(5,9);
- G.shortestPathDj(0);
- return 0;
- }
graph-Dijkstra's shortest-path alogorithm的更多相关文章
- (中等) 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 ...
- 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 ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 ...
随机推荐
- 如何使用在Windows 下AspNetCore Api 和 consul
在Windows 下如何使用 AspNetCore Api 和 consul https://blog.csdn.net/sD7O95O/article/details/80750803 一.概念:什 ...
- 简单记录下HTTPS中的SSL
大概思路 大概思路是混合加密的方式,即对称加密方式混合非对称加密方式. 非对称加密会更加安全,功能也更强大,但他复杂而且速度慢. 对称加密速度快,但要保证这个公共密钥的正确性和真实性. 所以两者结合, ...
- 042 Trapping Rain Water 接雨水
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...
- Django 的一些错误以及处理
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 589: 'static', expected 'e ...
- python实现批量远程执行命令及批量上传下载文件
#!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...
- JAVA基础之项目分包
个人理解: 项目分层分包适合多人开发合作的,最好一个界面设置一个view,同时注释一定设置好,按照顺序:从前向后进行传递参数,从后向前进行传递返回值来进行判断是否真正的执行了sql语句(可以不返回), ...
- 使用js获取复选框的值,并把数组传回后台处理,过程使用的是Ajax异步查询
这是界面代码: function shua(){ var id_array=new Array(); $('input[id="checkAll& ...
- Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.
Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.七宗罪:暴食.贪婪.懒惰.暴怒.傲慢.色欲.妒忌.
- Beta_版本发布
学号 姓名 201731041215 王阳 201731062302 鲜雨珂 201731062128 邓捷 201731062305 周蓉 201731062131 龙继平 201731062304 ...
- 【虚拟机-虚拟网络】使用 PsPing & PaPing 进行 TCP 端口连通性测试
PsPing & PaPing 介绍 通常,我们测试数据包能否通过 IP 协议到达特定主机时,都习惯使用 ping 命令.工作时 ping 向目标主机发送一个 IMCP Echo 请求的数据包 ...