Floyd算法简单实现(C++)
图的最短路径问题主要包括三种算法:
(3)Bellman (含有负权边的单源最短路径)
本文主要讲使用C++实现简单的Floyd算法,Floyd算法原理参见 Floyd–Warshall algorithm
Floyd算法简单实现(C++)
#include<iostream>
using namespace std; #define MAXVEX 10
#define INFINITY 65535 typedef int Patharc[MAXVEX][MAXVEX];
typedef int ShortPathTable[MAXVEX][MAXVEX]; typedef struct {
int vex[MAXVEX];
int arc[MAXVEX][MAXVEX];
int numVertexes;
} MGraph; // 构建图
void CreateMGraph(MGraph *G){
int i, j, k; // 初始化图
G->numVertexes = ;
for(i = ; i < G->numVertexes; ++i){
G->vex[i] = i;
}
for(i = ; i < G->numVertexes; ++i){
for(j = ; j < G->numVertexes; ++j){
if(i == j)
G->arc[i][j] = ;
else
G->arc[i][j] = G->arc[j][i] = INFINITY;
}
} G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ; G->arc[][] = ;
G->arc[][] = ; G->arc[][] = ; // 设置对称位置元素值
for(i = ; i < G->numVertexes; ++i){
for(j = i; j < G->numVertexes; ++j){
G->arc[j][i] = G->arc[i][j];
}
}
} // Floyd algorithm
void ShortPath_Floyd(MGraph G, Patharc P, ShortPathTable D){
int i, j, k;
// 二重循环,初始化P, D
for(i = ; i < G.numVertexes; ++i){
for(j = ; j < G.numVertexes; ++j){
D[i][j] = G.arc[i][j];
P[i][j] = j;
}
}
// 三重循环, Floyd algorithm
for(k = ; k < G.numVertexes; ++k){
for(i = ; i < G.numVertexes; ++i){
for(j = ; j < G.numVertexes; ++j){
if(D[i][j] > D[i][k]+D[k][j]){
D[i][j] = D[i][k]+D[k][j];
P[i][j] = P[i][k];
}
}
}
}
} // 打印最短路径
void PrintShortPath(MGraph G, Patharc P, ShortPathTable D){
int i, j, k;
cout<<"各顶点之间的最短路径如下: "<<endl;
for(i = ; i < G.numVertexes; ++i){
for(j = i+; j < G.numVertexes; ++j){
cout<<"v"<<i<<"--"<<"v"<<j<<" "<<"weight: "<<D[i][j]<<" Path: "<<i<<" -> ";
k = P[i][j];
while(k != j){
cout<<k<<" -> ";
k = P[k][j];
}
cout<<j<<endl;
}
cout<<endl;
}
} int main(int argc, char const *argv[]) {
MGraph G;
Patharc P;
ShortPathTable D;
CreateMGraph(&G);
ShortPath_Floyd(G, P, D);
PrintShortPath(G, P, D);
return ;
}
运行结果:
各顶点之间的最短路径如下:
v0--v1 weight: Path: ->
v0--v2 weight: Path: -> ->
v0--v3 weight: Path: -> -> -> ->
v0--v4 weight: Path: -> -> ->
v0--v5 weight: Path: -> -> -> ->
v0--v6 weight: Path: -> -> -> -> ->
v0--v7 weight: Path: -> -> -> -> -> ->
v0--v8 weight: Path: -> -> -> -> -> -> -> v1--v2 weight: Path: ->
v1--v3 weight: Path: -> -> ->
v1--v4 weight: Path: -> ->
v1--v5 weight: Path: -> -> ->
v1--v6 weight: Path: -> -> -> ->
v1--v7 weight: Path: -> -> -> -> ->
v1--v8 weight: Path: -> -> -> -> -> -> v2--v3 weight: Path: -> ->
v2--v4 weight: Path: ->
v2--v5 weight: Path: -> ->
v2--v6 weight: Path: -> -> ->
v2--v7 weight: Path: -> -> -> ->
v2--v8 weight: Path: -> -> -> -> -> v3--v4 weight: Path: ->
v3--v5 weight: Path: -> ->
v3--v6 weight: Path: ->
v3--v7 weight: Path: -> ->
v3--v8 weight: Path: -> -> -> v4--v5 weight: Path: ->
v4--v6 weight: Path: -> ->
v4--v7 weight: Path: -> -> ->
v4--v8 weight: Path: -> -> -> -> v5--v6 weight: Path: -> ->
v5--v7 weight: Path: ->
v5--v8 weight: Path: -> -> v6--v7 weight: Path: ->
v6--v8 weight: Path: -> -> v7--v8 weight: Path: -> [Finished in .2s]
参考资料:
Floyd–Warshall algorithm, Wikipedia
Floyd Warshall Algorithm | DP-16 , geeksforgeeks
Floyd算法简单实现(C++)的更多相关文章
- 算法笔记_069:Floyd算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 使用Floyd算法得到最短距离示例 2.2 具体编码 1 问题描述 何为Floyd算法? Floyd算法功能:给定一个加权连通图,求取从每一个顶点到其它所 ...
- POJ-2240(floyd算法简单应用)
Arbitrage poj-2240 #include<iostream> #include<cstdio> #include<cstring> #include& ...
- floyd算法 青云的机房组网方案(简单)
青云的机房组网方案(简单) 青云现在要将 nn 个机房连成一个互相连通的网络.工程师小王设计出一个方案:通过在 nn 个机房之间铺设 n-1n−1 条双向的光纤,将所有的机房连接.可以假设数据在两个机 ...
- CCF(通信网络):简单DFS+floyd算法
通信网络 201709-4 一看到题目分析了题意之后,我就想到用floyd算法来求解每一对顶点的最短路.如果一个点和任意一个点都有最短路(不为INF),那么这就是符合的一个答案.可是因为题目超时,只能 ...
- 最短路径之Floyd算法
Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...
- 最短路径问题——floyd算法
floyd算法和之前讲的bellman算法.dijkstra算法最大的不同在于它所处理的终于不再是单源问题了,floyd可以解决任何点到点之间的最短路径问题,个人觉得floyd是最简单最好用的一种算法 ...
- 最短路径——Floyd算法
如何求一张图中任意两顶点之间的最短路径长度,这里写一种最简单的算法——Floyd算法: #include<stdio.h> #define inf 9999 int main() { ][ ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- Floyd 算法的动态规划本质
[转载自:http://www.cnblogs.com/chenying99/p/3932877.html] Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(A ...
随机推荐
- MYSQL性能调优与架构设计之select count(*)的思考
select count(*)的思考 原文:MYSQL性能调优与架构设计 举例: 这里我们就拿一个看上去很简单的功能来分析一下. 需求:一个论坛帖子总量的统计 附加要求:实时更新 在很多人看来,这 ...
- RobotFramework自动化测试框架(3)- RobotFramework扩展测试库、资源文件、变量文件
扩展测试库 扩展测试库可使用python或java语言编写.后直接导入需要使用的测试用例文件即可. 具体的实现和操作,后续补充.请参考官网. 资源文件 在资源文件中定义用户关键字,它提供了共享机制,即 ...
- Centos6.8 配置 Tomcat
1.安装Tomcat,安装之前必须先安装Java,先安装java 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index. ...
- 跟我一起玩Win32开发(4):创建菜单
也不知道发生什么事情,CSDN把我的文章弄到首页,结果有不少说我在误人子弟,是啊,我去年就说过了,如果你要成为砖家级人物,请远离我的博客,我这个人没什么特长,唯一厉害的一点就是不相信权威,鄙视砖家,所 ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- LightOj 1088 - Points in Segments (二分枚举)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1088 题目描述: 给出一个n位数升序排列的数列,然后q个查询,每个查询问指定 ...
- ios NSFileManager 用法详解
转自:http://blog.csdn.net/ios_che/article/details/7287266 iPhone文件系统NSFileManager讲解是本文要介绍的内容,主要是通过ipho ...
- 关于发布WP 8.1应用信息不匹配问题的解决办法
错误提示: 与此更新关联的程序包标识符与已上传程序包中的标识符不匹配: The package identity associated with this update doesn't match ...
- iOS面试题之runloop
本文围绕以下几个部分展开对runloop的叙述. 1.runloop是什么/runloop的概念? 2.NSRunLoop 和 CFRunLoopRef? 3.runloop和线程的关系? 4.run ...
- AndroidStudio碰到的各种问题
源码已经下载了,但是为毛关联不了? 我的源码默认是下载在Sdk\sources\android-23\目录下面的,以前开发的时候都是自动关联的,今天碰到了怎么刷新,怎么关联都不行. 解决方式为: 1. ...