您的位置:首页 > 其它

最短路径Dijkstra(邻接矩阵)

2012-08-20 19:31 218 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: