# 图的最短路径算法Dijkstra算法

#include <stdio.h>
#include <stdlib.h>

#define QUEUE_MAXSIZE 30
#define VERTEX_MAX 26
#define MAXVALUE 99999

typedef struct
{
char Vertex[VERTEX_MAX];//保存顶点信息，序号或字母
int Edges[VERTEX_MAX][VERTEX_MAX];//保存边的权
int isTrav[VERTEX_MAX];//遍历标志
int VertexNum;//顶点的数量
int EdgeNum;//边的数量
int GraphType;//图的类型，0：无向图；1：有向图
}MatrixGraph;

void CreateMatrixGraph(MatrixGraph *G);//创建邻接矩阵图
void OutMatrix(MatrixGraph *G);//输出邻接矩阵

/************************************************************************

************************************************************************/
void CreateMatrixGraph(MatrixGraph *G)
{
int i, j, k, weight;
char start, end;

printf("输入各个顶点的信息\n");
for (i = 0; i < G->VertexNum; i++)
{
getchar();
printf("第%d个顶点：", i+1);
scanf("%c", &(G->Vertex[i]));
}

printf("输入构成各边的两个顶点及权值（用逗号分隔）:\n");
for (k = 0; k < G->EdgeNum; k++)
{
getchar();
printf("第%d条边:", k+1);
scanf("%c,%c,%d", &start, &end, &weight);
for (i = 0; start != G->Vertex[i]; i++);//在已有顶点中查找起始点
for (j = 0; end != G->Vertex[j]; j++);//在已有顶点中查找终点
G->Edges[i][j] = weight;
if (G->GraphType == 0)
G->Edges[j][i] = weight;
}
}
/************************************************************************

************************************************************************/
void OutMatrix(MatrixGraph *G)
{
int i, j;
for (j = 0; j < G->VertexNum; j++)
{
printf("\t%c", G->Vertex[j]);
}
printf("\n");
printf("      ------------------------------------\n");
for (i = 0; i < G->VertexNum; i++)
{
printf("    %c |", G->Vertex[i]);
for (j = 0; j < G->VertexNum; j++)
{
if (G->Edges[i][j] == MAXVALUE)
printf("\t∞");
else
printf("\t%d", G->Edges[i][j]);
}
printf("\n");
}
printf("\n");
}

/************************************************************************

************************************************************************/
void Dijkstra(MatrixGraph G)
{
int weight[VERTEX_MAX];
int path[VERTEX_MAX];
int tmpvertex[VERTEX_MAX];

int i, j, k, v0, min;

printf("输入源点的编号：");
scanf("%d", &v0);
v0--;
for (i =0; i < G.VertexNum; i++)
{
weight[i] = G.Edges[v0][i];//保存最小权值
if (weight[i] < MAXVALUE && weight[i] > 0)
path[i] = v0;
tmpvertex[i] = 0;//初始化顶点集合为空
}
tmpvertex[v0] = 1;//将顶点v0添加到集合U中
weight[v0] = 0;
for (i = 0; i < G.VertexNum; i++)
{
min = MAXVALUE;
k = v0;
for (j = 0; j < G.VertexNum; j++)
{
if (tmpvertex[j] == 0 && weight[j] < min)
{
min = weight[j];
k = j;
}
}
tmpvertex[k] = 1;
for (j = 0; j < G.VertexNum; j++)
{
if (tmpvertex[j] == 0 && weight[k] + G.Edges[k][j] < weight[j])
{
weight[j] = weight[k] + G.Edges[k][j];
path[j] = k;
}
}
}
printf("\n顶点%c到各个顶点的最短路径为(终点 -- 源点)：\n", G.Vertex[v0]);
for (i = 0; i < G.VertexNum; i++)
{
if (tmpvertex[i] == 1)
{
k = i;
while (k != v0)
{
j = k;
printf("%c -- ", G.Vertex[k]);
k = path[k];
}
printf("%c\n", G.Vertex[k]);
}
else
printf("%c -- %c:无路径\n", G.Vertex[i], G.Vertex[v0]);
}
}
/************************************************************************

************************************************************************/
int main()
{
MatrixGraph G;
int i, j;
char select;

do
{
printf("输入生成图的类型(0:无向图,1:有向图):");
scanf("%d", &G.GraphType);

printf("输入图的顶点数量和边数量:");
scanf("%d,%d", &G.VertexNum, &G.EdgeNum);

//清空矩阵
for (i = 0; i < G.VertexNum; i++)
for (j = 0; j < G.VertexNum; j++)
G.Edges[i][j] = MAXVALUE;

CreateMatrixGraph(&G);
printf("邻接矩阵数据如下:\n");
OutMatrix(&G);

printf("最短路径:\n");
Dijkstra(G);
printf("继续进行吗？(Y/N)");
scanf("%c", &select);
getchar();
}while (select != 'N' && select != 'n');

getchar();
getchar();
return 0;
}  

• 本文已收录于以下专栏：

举报原因： 您举报文章：图的最短路径算法Dijkstra算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)