C++简单实现Dijkstra算法

所属分类: 软件编程 / C 语言 阅读数: 86
收藏 0 赞 0 分享

本文实例为大家分享了C++简单实现Dijkstra算法的具体代码,供大家参考,具体内容如下

// Dijkstra.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include <iostream>
#include <stack>
#define MAX_VALUE 1000
using namespace std;
 
struct MGraph
{
 int *edges[MAX_VALUE];
 int iVertexCount, iEdageCount;
};
void ReadDate(MGraph *mGraph, int *iBegin, int *iEnd);
void Dijkstra(MGraph *mGraph, int *pArrDis, int *pArrPath, int iBegin);
void PrintResult(int *pArrDis, int *pArrPath, int iBegin, int iEnd);
 
int main()
{
 int iBegin, iEnd;
 int *pArrPath = new int[MAX_VALUE];
 int *pArrDis = new int[MAX_VALUE];
 MGraph mGraph;
 for (int i = 0; i < MAX_VALUE; i++){
 mGraph.edges[i] = new int[MAX_VALUE];
 }
 ReadDate(&mGraph, &iBegin, &iEnd);
 Dijkstra(&mGraph, pArrDis, pArrPath, iBegin);
 PrintResult(pArrDis,pArrPath, iBegin, iEnd);
 system("pause");
 return 0;
}
 
void ReadDate(MGraph *mGraph, int *iBegin, int *iEnd){
 cout << "请输入顶点数量" << endl;
 cin >> mGraph->iVertexCount;
 cout << "请输入邻接矩阵数据:" << endl;
 for (int iRow = 1; iRow <= mGraph->iVertexCount; iRow++){
 for (int iCol = 1; iCol <= mGraph->iVertexCount; iCol++){
  cin >> mGraph->edges[iRow][iCol];
 }
 }
 
 //cout << "请输入顶点数和边数" << endl;
 //cin >> mGraph->iVertexCount >> mGraph->iEdageCount;
 //for (int iRow = 1; iRow <= mGraph->iVertexCount; iRow++){
 // for (int iCol = 1; iCol <= mGraph->iVertexCount; iCol++){
 // mGraph->edges[iRow][iCol] = -1;
 // }
 //}
 //cout << "请输入连通边及权重" << endl;
 //int iRow, iCol, iWeight;
 //for (int i = 1; i <= mGraph->iEdageCount; i++){
 // cin >> iRow >> iCol >> iWeight;
 // mGraph->edges[iRow][iCol] = iWeight;
 //}
 
 cout << "请输入查询的起点和终点" << endl;
 cin >> *iBegin >> *iEnd;
}
 
void Dijkstra(MGraph *mGraph, int *pArrDis, int *pArrPath, int iBegin){
 int iMin;
 int bArrVisited[MAX_VALUE];
 memset(bArrVisited, false, sizeof(bArrVisited));
 for (int i = 1; i <= mGraph->iVertexCount; i++){
 pArrPath[i] = -1;
 mGraph->edges[i][i] = 0;
 pArrDis[i] = mGraph->edges[iBegin][i] != -1 ? mGraph->edges[iBegin][i] : INT_MAX;
 }
 int iNewCost;
 int iSelected = iBegin;
 
 for (int i = 1; i <= mGraph->iVertexCount; i++){
 int iPre = iSelected;
 iMin = INT_MAX;
 for (int j = 1; j <= mGraph->iVertexCount; j++){
  if (!bArrVisited[j] && pArrDis[j] < iMin){
  iMin = pArrDis[j];
  iSelected = j;
  }
 }
 for (int j = 1; j <= mGraph->iVertexCount; j++){
  iNewCost = pArrDis[iSelected] != -1 && mGraph->edges[iSelected][j] != -1 ? pArrDis[iSelected] + mGraph->edges[iSelected][j] : INT_MAX;
  if (!bArrVisited[j] && iNewCost < pArrDis[j]){
  pArrPath[j] = iSelected;
  pArrDis[j] = iNewCost;
  //pArrPath[iSelected] = iSelected;
  }
 }
 //pArrPath[iSelected] = iPre;
 bArrVisited[iSelected] = true;
 }
}
 
void PrintResult(int *pArrDis, int *pArrPath, int iBegin, int iEnd){
 
 cout << "从" << iBegin << "开始到" << iEnd << "的最短路径长度为";
 cout << pArrDis[iEnd] << endl;
 cout << "所经过的最短路径节点为:";
 
 stack<int> stackVertices;
 int k = iEnd;
 do{
 k = pArrPath[k];
 stackVertices.push(k);
 } while (k != pArrPath[k] && k != -1);
 cout << stackVertices.top()*-1;
 stackVertices.pop();
 
 unsigned int nLength = stackVertices.size();
 for (unsigned int nIndex = 0; nIndex < nLength; nIndex++)
 {
 cout << " -> " << stackVertices.top();
 stackVertices.pop();
 }
 cout << " -> " << iEnd << endl;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

利用C语言来求最大连续子序列乘积的方法

这篇文章主要介绍了利用C语言来求最大连续子序列乘积的方法,基本的思路以外文中还附有相关ACM题目,需要的朋友可以参考下
收藏 0 赞 0 分享

用C语言判断一个二叉树是否为另一个的子结构

这篇文章主要介绍了用C语言判断一个二叉树是否为另一个的子结构,是数据结构学习当中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现的阶乘,排列和组合实例

这篇文章主要介绍了C语言实现的阶乘,排列和组合的方法,涉及C语言数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言查找数组里数字重复次数的方法

这篇文章主要介绍了C语言查找数组里数字重复次数的方法,涉及C语言针对数组的遍历与判断技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言简单实现计算字符个数的方法

这篇文章主要介绍了C语言简单实现计算字符个数的方法,涉及C语言针对字符串的简单遍历与判定技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

c实现linux下的数据库备份

本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

C++获得文件状态信息的方法

这篇文章主要介绍了C++获得文件状态信息的方法,包括文件状态信息、文件所在磁盘盘符、文件创建时间、访问时间及修改日期等,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言按关键字搜索文件夹中文件的方法

这篇文章主要介绍了C语言按关键字搜索文件夹中文件的方法,涉及C语言文件操作及字符串查找的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言之字符串模糊查询方法的实现

本篇文章主要为大家介绍字符串模糊查询的C语言程序编写方法,有需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现BMP转换JPG的方法

这篇文章主要介绍了C语言实现BMP转换JPG的方法,涉及C#图片格式转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多