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 ...
随机推荐
- Net Core -- 配置Kestrel端口
Net Core -- 配置Kestrel端口 Kestrel介绍 在Asp.Net Core中,我们的web application 其实是运行在Kestrel服务上,它是一个基于libuv开源的跨 ...
- 042 Trapping Rain Water 接雨水
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...
- 使用jstack分析解决进程死锁问题
项目启动后不久就会出现死锁的现象,一直不知道什么原因造成的,后来经过大神的指点,解决了这个问题. 流程如下: 1.环境jdk1.6以上: 2.linux下使用ps aux|grep tomcat 命令 ...
- SpringMVC注解方式与文件上传
目录: springmvc的注解方式 文件上传(上传图片,并显示) 一.注解 在类前面加上@Controller 表示该类是一个控制器在方法handleRequest 前面加上 @RequestMap ...
- JVM类加载机制一
类加载的过程 什么是类加载?Java编译器会将我们编写好的代码编译成class字节码文件,JVM会把这些class字节码文件加载到内存中,并对加载的数据进行校验.准备.解析并初始化,这个过程就是类加载 ...
- 如何删除 CentOS 6 更新后产生的多余的内核?
第一种方法:通过命令的方式解决多余的内核 1.首先查看当前内核的版本号: [root@jxatei ~]# uname -a Linux jxatei.server2.6.32-573.1.1.el ...
- 小目标 | Power BI新人快速上手手册
· 适用人群:数据分析专业人士,在数据分析方向需求发展人士 · 应用场景:数据汇报.数据可视化展现.数据建模分析 · 掌握难度:★★★★☆ 本期讲师 『PowerPivot工坊』公众号提供Power ...
- ThreadLocal的内存泄露
ThreadLocal的目的就是为每一个使用ThreadLocal的线程都提供一个值,让该值和使用它的线程绑定,当然每一个线程都可以独立地改变它绑定的值.如果需要隔离多个线程之间的共享冲突,可以使用T ...
- 洛谷 P2733 家的范围 Home on the Range
题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因为一些原因,他的奶牛只在正方形的牧场上吃草.)遗憾的是,他的奶牛已经毁坏一些土地.( 一 ...
- Android(java)学习笔记126:判断SD卡状态和SD卡容量
1. 判断SD卡状态和SD卡存储空间大小 当我们在使用SD卡时候,如果我们想往SD卡里读写数据,我们必须在这之前进行一个逻辑判断,那就是判断SD卡状态和SD存储空间大小: 核心代码: String s ...