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

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

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的更多相关文章

  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关闭端口方法

    windows关闭端口方法 在介绍各种端口的作用前,这里先介绍一下在Windows中如何关闭/打开端口,因为默认的情况下,有很多不安全的或没有什么用的端口是开启的,比如Telnet服务的23端口.FT ...

  2. Announcing .NET Core 2.1

    Announcing .NET Core 2.1 https://blogs.msdn.microsoft.com/dotnet/2018/05/30/announcing-net-core-2-1/ ...

  3. net core (上)

    net core (上) 本文是基于Windows10的. 下载地址: https://code.visualstudio.com/ insider 版下载地址: https://code.visua ...

  4. java exception "file not found or file not exist"

    出现这种异常一般有两种原因,第一种就是文件真的不存在:第二种是权限问题,权限问题又分为文件本身的权限和包含它的文件夹的权限 比如 ~/aaa/bbb/ccc/ddd/eee.txt  只要 aaa , ...

  5. nio aio netty区别

    传统io就是bio     同步阻塞         但可以采用伪同步 nio  jdk1.7以前     同步非阻塞io     1.7以后     同步异步非阻塞                  ...

  6. 一、Postgresql的基本操作

    ---------------------------------------------------------------------------------------------------- ...

  7. mongodb的投影

    mongodb 投影意思是只选择必要的数据而不是选择一个文件的数据的整个.如果一个文档有5个字段,需要显示只有3个,然后选择其中只有3个字段. find() 方法 MongoDB 的find()方法, ...

  8. Class 学习 (Es6阮一峰)

    es5 构造函数 实例: function Point(x, y) { this.x = x; this.y = y; } Point.prototype.toString = function () ...

  9. eclipse、idea安装lombok插件

    今天新项目中,用到了lombok知识,很方便. 但是使用之前,需要在eclipse.idea中安装插件才可以使用,配置如下.  一:在开发工具中安装插件: Eclipse: 下载地址:https:// ...

  10. ionic 2 起航 控件的使用 客户列表场景(二)

    首先放出我hithub项目代码例子,有兴趣研究探讨的同学可以去看看 https://github.com/linyuebin2016/ionic2.git 下面我们来尝试下第一个项目场景 一份客户的列 ...