最短路算法
来源:互联网 发布:织梦cms百科 编辑:程序博客网 时间:2024/06/07 06:31
根据大话数据结构整理
/***********************************输入:邻接矩阵vMatrix*输出:最短路径***********************************/#include <iostream>#include <vector>using namespace std;#define INF 65535/*********Dijkstra************/void Dijstra(vector<vector<int>>&Matrix, int v0){ vector<int> patharc(Matrix.size()); vector<int> shortPathTable(Matrix.size()); vector<bool> visit(Matrix.size()); for (unsigned i = 0; i < Matrix.size(); i++) shortPathTable[i] = Matrix[v0][i]; visit[v0] = true;//v0点已访问 for (unsigned v = 1; v < Matrix.size(); v++)//只需要找到n-1个点组成路径 { //找到所有未访问点最小权值 int min = INF; int k; for (unsigned w = 0; w < Matrix.size(); w++) { if (!visit[w] && shortPathTable[w] < min) { k = w; min = shortPathTable[w]; } } visit[k] = true; //更新权值 for (unsigned w = 0; w < Matrix.size(); w++) { if (!visit[w] && (min + Matrix[k][w] < shortPathTable[w])) { shortPathTable[w] = min + Matrix[k][w]; patharc[w] = k;//表示w的前驱点是k } } } //输出路径 cout << "(Dijstra)顶点" << v0 << "到各点的最短路径:" << endl; vector<vector<int>> vvOut(Matrix.size()); for (unsigned i = 0; i < vvOut.size(); i++) { int end = i; vvOut[i].push_back(i); while (end != v0) { end = patharc[end]; vvOut[i].push_back(end); } if (i == v0) vvOut[i].push_back(v0); } for (unsigned i = 0; i < vvOut.size(); i++) { auto iter = vvOut[i].rbegin(); cout << *iter++; for (; iter != vvOut[i].rend(); iter++) cout << "->" << *iter; cout << endl; }}/*********Floyd****************/void Floyd(vector<vector<int>>&Matrix){ //初始化 vector<vector<int>> pathMatrix(Matrix.size(), vector<int>(Matrix.size())); vector<vector<int>> shortPathTable(Matrix); for (unsigned v = 0; v < Matrix.size(); v++) for (unsigned w = 0; w < Matrix.size(); w++) pathMatrix[v][w] = w; //3层循环 for (unsigned k = 0; k < Matrix.size(); k++) { for (unsigned v = 0; v < Matrix.size(); v++) { for (unsigned w = 0; w < Matrix.size(); w++) { if (shortPathTable[v][w] > shortPathTable[v][k] + shortPathTable[k][w]) { shortPathTable[v][w] = shortPathTable[v][k] + shortPathTable[k][w]; pathMatrix[v][w] = pathMatrix[v][k]; } } } } for (unsigned v = 0; v < Matrix.size(); v++) { cout << "(Floyd)顶点" << v << "到各点的最短路径:" << endl; for (unsigned w = 0; w < Matrix.size(); w++) { cout << v; int end = pathMatrix[v][w]; while (end != w) { cout << "->" << end; end = pathMatrix[end][w]; } cout << "->" << w << endl; } }}/*********主函数****************/int main(){ int nodeNum, N, data1, data2, data3; cin >> nodeNum >> N; /******建立邻接矩阵**********/ vector<vector<int>> vMatrix(nodeNum, vector<int>(nodeNum, INF)); for (int j = 0; j < nodeNum; j++) vMatrix[j][j] = 0; for (int i = 0; i < N; i++) { cin >> data1 >> data2 >> data3; vMatrix[data1][data2] = data3; vMatrix[data2][data1] = data3; } Dijstra(vMatrix, 0); Floyd(vMatrix); return 0;}/*case:顶点数n,边数m,m条边*//*9150 1 100 5 111 2 181 8 121 6 162 3 222 8 83 4 203 7 163 6 243 8 214 5 264 7 75 6 176 7 19*/
阅读全文
0 0
- JavaScript最短路算法
- 最短路算法总结
- 最短路算法总结
- 最短路 Dijkstra算法
- 最短路算法
- 最短路算法
- 最短路-Floyd算法
- 算法训练 最短路
- 最短路算法Dijkstra
- 最短路算法解析
- 最短路--SPFA算法
- 算法训练 最短路
- 最短路kruskal算法
- 最短路SPFA算法
- Dijkstra最短路算法
- 最短路算法
- 最短路算法汇总
- 最短路算法
- ConfigParser模块详解
- Qt5.9.1+mingw32静态编译
- spoj COT
- BZOJ 1007 几何 解题报告
- 网络流24题-4
- 最短路算法
- Docker私有仓库搭建
- SpringAop--切面实例
- 递推和递归一
- 剑指offer:数组中重复的数字
- 用两个栈实现队列java实现
- yum 和 rpm安装mysql彻底删除
- JAVA对象的创建过程
- [LeetCode] 3Sum的O(N^2)解法