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

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

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. Hive进阶_Hive的客户端操作

    启动远程客户端 # hive --service hiveserver2获取连接-〉创建运行环境-〉执行HQL-〉处理结果-〉释放资源 工具类 package demo.utils; import j ...

  2. Hive_Hive的数据模型_分区表

    Hive的数据模型之分区表 准备数据表: create table sampledata (sid int, sname string, gender string, language int, ma ...

  3. Java编码优化

    Java编码优化 1.尽可能使用局部变量 调用方法时传递的参数以及在调用中创建的临时变量都保存在栈中速度较快,其他变 量,如静态变量.实例变量等,都在堆中创建,速度较慢.另外,栈中创建的变量,随 着方 ...

  4. java wait(),notify(),notifyAll()

    wait()的作用是使当前执行代码的线程进行等待,此方法是Object类的方法,该方法用来将当前线程置入“预执行队列”中,并且在wait()所带的代码处停止执行,直到接到通知或被中断位置.在调用wai ...

  5. ES6学习(2)

    Set和Map数据结构 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const s = new ...

  6. Servlet--HttpServlet

    一.Servlet 接口(javax.servlet) 定义:     public interface Servlet      Implemented by: FacesServlet, Gene ...

  7. Android开发学习——小细节注意

    Android中通过Intent调用其他应用的方法(转) Android中两种序列化方式的比较Serializable和Parcelable http://www.jcodecraeer.com/a/ ...

  8. Microsoft Exchange本地和Exchange Online可以与第三方服务共享

    很多人都知道Office 365中的Microsoft Exchange本地和Exchange Online可以与第三方服务共享您的个人数据?例如,在Exchange电子邮件中找到的任何地图地址都会发 ...

  9. Python+Selenium之摘取网页上全部邮箱

    本文转载:http://blog.csdn.net/u011541946/article/details/68485981 练习场景:在某一个网页上有些字段是我们感兴趣的,我们希望摘取出来,进行其他操 ...

  10. Python +selenium之集成测试报告与unittest单元测试

    随着软件不断迭代,对应的功能也会越来越多,从而对应的测试用例也会呈指数增长.如果将全部的测试用例集成在一个文件中就会显得特别的臃肿而且维护成本也会很高. 一个很好的放大就是将这些测试yo你给里按照功能 ...