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

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

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. Win10专业版系统下添加其他国家语言

    Win10专业版系统下如何添加其他国家语言?国内的win10专业版系统默认情况下是安装简体中文,但是有的用户出于工作原因需要使用其它字体.比如外国友人就需要使用英语,西班牙等.其实win10专业版是支 ...

  2. Storm概念学习系列之并行度与如何提高storm的并行度

    不多说,直接上干货! 对于storm来说,并行度的概念非常重要!大家一定要好好理解和消化. storm的并行度,可以简单的理解为多线程. 如何提高storm的并行度? storm程序主要由spout和 ...

  3. Joda-Time简介

    Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time.可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成. Joda-Ti ...

  4. 走进docker的世界之入门篇

    by zhouzhipeng from https://blog.zhouzhipeng.com/walk-in-docker-beginning.html本文可全文转载,但需要保留原作者和出处. 什 ...

  5. Code First 2

    在codefirst一中也说了Mapping是实体与数据库的纽带,model通过Mapping映射到数据库,我们可以从数据库的角度来分析?首先是映射到数据库,这个是必须的.数据库里面一般包括表.列.约 ...

  6. ABAP日期和时间运算

    "日期运算是以天为单位,时间运算以秒为单位.DATA:date1 TYPE d. "服务器当前日期date1 = sy-datum.WRITE: / date1 . "2 ...

  7. 零基础逆向工程22_PE结构06_导入表

    导入表结构 typedef struct _IMAGE_IMPORT_DESCRIPTOR { union { DWORD Characteristics; DWORD OriginalFirstTh ...

  8. ios 身份证照片识别信息

    一个近乎完整的可识别中国身份证信息的Demo就问问你霸气不

  9. 编写xcode5插件需要增加DVTPlugInCompatibilityUUIDs

    之前使用的xcode4.6的插件在升级到xcode5后不能使用了,查了很多资料,终于知道是缺少了DVTPlugInCompatibilityUUIDs 请在插件项目plist文件中加入DVTPlugI ...

  10. get_user

    Name get_user --    Get a simple variable from user space. Synopsis get_user ( x, ptr); Arguments x ...