编程算法 - 单源最短路问题 Bellman-Ford 代码(C)
来源:互联网 发布:中国邮政网络技术学院 编辑:程序博客网 时间:2024/04/29 00:32
单源最短路问题 Bellman-Ford 代码(C)
本文地址: http://blog.csdn.net/caroline_wendy
单源最短路: 固定一个起点, 求它到其他所有点的最短路的问题.
Bellman-Ford: 设当前到顶点i的最短路长度为d[i], 并设初值d[s]=0, d[i]=INF,
再不断使用递推关系式d[e.to] = d[e.from] + e.cost更新d的值.
时间复杂度: O(V*E)
代码:
/* * CppPrimer.cpp * * Created on: 2013.11.12 * Author: Caroline *//*eclipse cdt*/#include <stdio.h>#include <limits.h>#include <vector>using namespace std;class Program {static const int MAX_V = 100;static const int MAX_E = 100;const int INF = INT_MAX>>2;struct edge {int from, to, cost; };edge es[MAX_E] = {{0,1,2}, {0,2,5},{1,0,2}, {1,2,4}, {1,3,6}, {1,4,10},{2,0,5}, {2,1,4}, {2,3,2},{3,2,2}, {3,1,6}, {3,5,1},{4,1,10}, {4,5,3}, {4,6,5},{5,3,1}, {5,4,3}, {5,6,9},{6,4,5}, {6,5,9}};int V = 7, E = 20;public:int d[MAX_V];void shortest_path(int s) {for (int i=0; i<V; ++i) d[i] = INF;d[s] = 0;while (true) {bool update = false;for (int i=0; i<E; i++) {edge e = es[i];if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {d[e.to] = d[e.from] + e.cost;update = true;}}if (!update) break;}}};int main (void){Program iP;iP.shortest_path(0);for (int i=0; i<7; i++) {printf("%d = %d\n", i, iP.d[i]);}return 0;}
输出:
0 = 01 = 22 = 53 = 74 = 115 = 86 = 16
7 0
- 编程算法 - 单源最短路问题 Bellman-Ford 代码(C)
- 单源最短路问题BellMan-Ford算法C++实现关键代码
- 单源最短路问题 bellman-ford算法
- 单源最短路问题 Bellman-Ford算法
- 单源最短路 Bellman-Ford算法
- [单源最短路]Bellman-Ford算法
- C--最短路(Bellman-Ford算法)
- C--最短路(Bellman-Ford算法)
- Bellman-Ford 算法 & SPFA(单源最短路问题)
- 算法学习之Bellman-Ford单源最短路问题
- 练习题 No.22 单源最短路问题(Bellman-Ford算法)
- 最短路问题(Bellman-Ford算法)
- 最短路问题 Bellman-Ford算法
- 图论--最短路问题--Bellman-Ford算法
- 单源最短路(Bellman-Ford) 实现代码
- 单源最短路算法 Dijstra Bellman-Ford
- 单源最短路Bellman-Ford
- 单源最短路 Bellman-Ford
- RMI:remote method invocation
- Jsp学习——filter的使用
- hdu 1533 Going Home (KM算法)
- nginx 学习
- 进程间通信______信号
- 编程算法 - 单源最短路问题 Bellman-Ford 代码(C)
- atitit.编辑表单的实现最佳实践dwr jq easyui
- POJ 2481 Cows
- 用*号在屏幕上打印出sin函数图像
- String与StringBuilder的区别
- hdu 2767 强联通
- Windows 7下硬盘安装Ubuntu 13.04图文教程
- Android 适配器教程(二)
- HDU 1166