最短路徑Dijkstra(鄰接矩陣)


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <Windows.h>

using namespace std;

#define INFINITY 65535
#define MAX_VERTEX_NUM 20  //頂點最多個數
#define LENGTH 5           //頂點字符長度

//*********************************鄰接矩陣***********************************begin
//鄰接矩陣
typedef struct _Graph
{
    int matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
    char vexs[MAX_VERTEX_NUM][LENGTH];
    int vexnum;
    int arcs;
}Graph;

int LocateVex(const Graph & g, char name[LENGTH])
{
    for (int i = 0; i < g.vexnum; i++)
    {
        if (0 == strcmp(g.vexs[i], name))
        {
            return i;
        }
    }
    return -1;
}

//圖的建造
void CreateGraph(Graph &g)
{
    ifstream fcin(_T("dijkstra.txt"));
    fcin>>g.vexnum;
    for (int i = 0; i < g.vexnum; i++)
    {
        for (int j = 0; j < g.vexnum; j++)
        {
            g.matrix[i][j] = INFINITY;
        }
    }
    for (int i = 0; i < g.vexnum; i++)
    {
        fcin>>g.vexs[i];
    }
    fcin>>g.arcs;
    char arcHead[LENGTH];
    char arcTail[LENGTH];
    int weight;
    for (int i = 0; i < g.arcs; i++)
    {
        memset(arcHead, 0, LENGTH);
        memset(arcTail, 0, LENGTH);
        fcin>>arcTail>>arcHead>>weight;
        int x = LocateVex(g, arcHead);
        int y = LocateVex(g, arcTail);
        //g.matrix[x][y] = weight;
        g.matrix[y][x] = weight;
    }
}

//************************************鄰接矩陣**************************************end
//************************************最短路徑************************************begin typedef int PathMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; typedef int ShortPathTable[MAX_VERTEX_NUM]; void ShortestPath_DIJ(Graph g, int v0, PathMatrix &P, ShortPathTable &D) { bool final[MAX_VERTEX_NUM]; int v = 0; for (; v < g.vexnum; v++) { final[v] = false; D[v] = g.matrix[v0][v]; for (int w = 0; w < g.vexnum; w++) { P[v][w] = false; } if (D[v] < INFINITY) { P[v][v0] = true; P[v][v] = true; } } D[v0] = 0; final[v0] = true; for (int i = 1; i < g.vexnum; i++) { int min = INFINITY; for (int w = 0; w < g.vexnum; w++) { if (!final[w]) { if (D[w] < min) { v = w; min = D[w]; } } } final[v] = true; for (int w = 0; w < g.vexnum; w++) { if (!final[w] && (min + g.matrix[v][w]) < D[w]) { D[w] = min + g.matrix[v][w]; for (int j = 0; j < g.vexnum; j++) { P[w][j] = P[v][j]; } P[w][w] = true; } } } } //************************************最短路徑************************************end //輔助函數,設置控制台的顏色 void SetConsoleTextColor(WORD dwColor) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); if (INVALID_HANDLE_VALUE == handle) { return; } SetConsoleTextAttribute(handle, dwColor); } int _tmain(int argc, _TCHAR* argv[]) { Graph graph; CreateGraph(graph); PathMatrix P; ShortPathTable D; SetConsoleTextColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); cout<<"************************最短路徑**************************"<<endl<<endl; ShortestPath_DIJ(graph, 0, P, D); cout<<"最短路徑數組p[i][j]如下:"<<endl; for(int i = 0; i < graph.vexnum; ++i) { for(int j=0;j<graph.vexnum;++j) cout<<P[i][j]<<" "; cout<<endl; } cout<<"源點到各頂點的最短路徑長度為:"<<endl; for(int i = 1;i < graph.vexnum; ++i) { if (INFINITY == D[i]) { cout<<graph.vexs[0]<<" "<<graph.vexs[i]<<""<<endl; } else { cout<<graph.vexs[0]<<" "<<graph.vexs[i]<<" "<<D[i]<<endl; } } return 0; }

界面運行如下:

建造圖用到的dijkstra.txt如下:

6
V0 V1 V2 V3 V4 V5
8
V0 V2 10
V0 V4 30
V0 V5 100
V1 V2 5
V2 V3 50
V3 V5 10
V4 V3 20
V4 V5 60


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM