C++簡(jiǎn)單實(shí)現(xiàn)Dijkstra算法
更新時(shí)間:2020年05月27日 15:13:45 作者:銘霏
這篇文章主要為大家詳細(xì)介紹了C++簡(jiǎn)單實(shí)現(xiàn)Dijkstra算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C++簡(jiǎn)單實(shí)現(xiàn)Dijkstra算法的具體代碼,供大家參考,具體內(nèi)容如下
// Dijkstra.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // #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 << "請(qǐng)輸入頂點(diǎn)數(shù)量" << endl; cin >> mGraph->iVertexCount; cout << "請(qǐng)輸入鄰接矩陣數(shù)據(jù):" << endl; for (int iRow = 1; iRow <= mGraph->iVertexCount; iRow++){ for (int iCol = 1; iCol <= mGraph->iVertexCount; iCol++){ cin >> mGraph->edges[iRow][iCol]; } } //cout << "請(qǐng)輸入頂點(diǎn)數(shù)和邊數(shù)" << 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 << "請(qǐng)輸入連通邊及權(quán)重" << endl; //int iRow, iCol, iWeight; //for (int i = 1; i <= mGraph->iEdageCount; i++){ // cin >> iRow >> iCol >> iWeight; // mGraph->edges[iRow][iCol] = iWeight; //} cout << "請(qǐng)輸入查詢的起點(diǎn)和終點(diǎn)" << 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 << "的最短路徑長(zhǎng)度為"; cout << pArrDis[iEnd] << endl; cout << "所經(jīng)過(guò)的最短路徑節(jié)點(diǎn)為:"; 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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
關(guān)于C++中的static關(guān)鍵字的總結(jié)
C++的static有兩種用法:面向過(guò)程程序設(shè)計(jì)中的static和面向?qū)ο蟪绦蛟O(shè)計(jì)中的static。前者應(yīng)用于普通變量和函數(shù),不涉及類;后者主要說(shuō)明static在類中的作用2013-09-09OpenCV 圖像拼接和圖像融合的實(shí)現(xiàn)
本文主要介紹了OpenCV 圖像拼接和圖像融合,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08C++類模板與函數(shù)模板基礎(chǔ)詳細(xì)講解
C++語(yǔ)言的模板技術(shù)包括函數(shù)模板和類模板,模板技術(shù)是一種代碼重用技術(shù),函數(shù)和類是C++語(yǔ)言中兩種主要的重用代碼形式,這篇文章主要介紹了C++函數(shù)模板和類模板,需要的朋友可以參考下2022-08-08直觀理解C語(yǔ)言中指向一位數(shù)組與二維數(shù)組的指針
這篇文章主要介紹了直觀理解C語(yǔ)言中指向一位數(shù)組與二維數(shù)組的指針,數(shù)組指針是C語(yǔ)言入門學(xué)習(xí)過(guò)程中的重點(diǎn)和難點(diǎn),需要的朋友可以參考下2016-05-05C語(yǔ)言實(shí)現(xiàn)線索二叉樹的定義與遍歷示例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)線索二叉樹的定義與遍歷,結(jié)合具體實(shí)例形式分析了基于C語(yǔ)言的線索二叉樹定義及遍歷操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2017-06-06