最短路径问题
在《图的存储与遍历》的基础上,实现求最短路径的Dijstra算法,并将该算法用于求下图从顶点A到其它各顶点的最短路径,以验证算法的正确性。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxVertexNum 20 //最大定点数设为100
#define MAXSIZE 20
#define INF 999 //定义无穷大
/****************图的邻接矩阵定义 *******************/
typedef char VertexType; //定点类型设为字符型
typedef int EdgeType; //边的权值设为字符型
typedef struct
{
VertexType vexs[MaxVertexNum]; //顶点表
EdgeType edges[MaxVertexNum][MaxVertexNum]; //邻接矩阵,即边表
int n; //n为顶点数
int e; //e为边数
}MGraph;
/****************图的邻接表定义 *******************/
typedef struct node //边表结点
{
int adjvex; //邻接点域
EdgeType info; //数据域
struct node *next; //指向下一个邻接点的指针域
} EdgeNode;
typedef struct vnode //顶点表结点
{
VertexType vertex; //顶点域
EdgeNode *firstedge; //边表头指针
}VertexNode;
typedef struct
{
VertexNode AdjList[MaxVertexNum]; //顶点表
int n; //顶点表顶点数
int e; //顶点表边数
}ALGraph;
/****************队列的类型及各种操作 *******************/
typedef int datatype;
typedef struct Queue
{
datatype data[MAXSIZE];
datatype top;
datatype bottom;
}Queue;
//初始化
void InitQueue(Queue * p)
{
p->top = 0;
p->bottom = 0;
}
//入队
void EnQueue(Queue *p,datatype val)
{
p->data[p->bottom] = val;
(p->bottom)++;
p->bottom = (p->bottom) % MAXSIZE;
}
//出队
da