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 ...
随机推荐
- YCOJ黑熊过河
Description 有一只黑熊想过河,但河很宽,黑熊不会游泳,只能借助河面上的石墩跳过去,他可以一次跳一墩,也可以一次跳两墩,但是每起跳一次都会耗费一定的能量,黑熊最终可能因能量不够而掉入水中,所 ...
- NSA互联网公开情报收集指南:迷宫中的秘密·下
猫宁!!! 参考链接: https://www.nsa.gov/news-features/declassified-documents/assets/files/Untangling-the-Web ...
- 使用selesium和pytesseract识别验证码,达到登录网页目的
关于验证码问题,大多可以在网上了解到目前有四种解决方案:1.开发注释验证码2.开发开一个“后门”,设置一个万能码,输入万能码则通过3.通过cookies绕过验证码4.图形识别技术 前三种是比较快速也是 ...
- Zookeeper(1、3、5节点)集群安装
1节点 1 week110的zookeeper的安装 + zookeeper提供少量数据的存储 3节点 hadoop-2.6.0.tar.gz的集群搭建(3节点) hadoop-2.6.0-cdh5. ...
- c++继承汇总(单继承、多继承、虚继承、菱形继承)
多重继承中,一个基类可以在派生层次中出现多次,如果一个派生类有多个直接基类,而这些直接基类又有一个共同的基类,则在最终的派生类中会保留该间接共同基类数据成员的多分同名成员.C++提供虚基类的方法使得在 ...
- AtCoder Regular Contest 062 E - AtCoDeerくんと立方体づくり / Building Cubes with AtCoDeer
题目传送门:https://arc062.contest.atcoder.jp/tasks/arc062_c 题目大意: 给你\(N\)块正方形木板,每块木板四角有四种颜色(可以相同),木板中央有编号 ...
- Planning CodeForces - 854C
Planning CodeForces - 854C 题意:有n架航班,第i架原先的时候是在第i分钟起飞的.现在前k分钟无法有飞机起飞,因此需要调整安排表,延后飞机起飞.仍然要求每一分钟只有一架飞机起 ...
- 模拟 HDOJ 5099 Comparison of Android versions
题目传送门 /* 题意:比较型号的大小 模拟:坑点在长度可能为5,此时设为'A' */ #include <cstdio> #include <algorithm> #incl ...
- TNS-12508 When Issuing Any SET Command For The Listene
TNS-12508 When Issuing Any SET Command For The Listener fact: Oracle Net Services fact: TNS Liste ...
- 转 PHP文件上传$_FILES数组各键值含义说明
文件上传的html表单: <form enctype="multipart/form-data" action="" method="POST& ...