校园导航系统

void Dijkstra(AdjMGraph G, int v0, int distance[], int path[])
{
int n = G.Vertices.size;
int *s = (int *)malloc(sizeof(int)*n);
int minDis, i, j, u;
for (i = 0; i < n; i++)

{
	distance[i] = G.edge[v0][i];
	s[i] = 0;
	if (i != v0 && distance[i] < MaxWeight)
		path[i] = v0;
	else path[i] = -1;
}
s[v0] = 1; //标记顶点v0已从集合T加入集合S中
//在当前还未找到最短路径的顶点集中选取具有最短距离的顶点u
for (i = 1; i < n; i++)
{
	minDis = MaxWeight;
	for (j = 0; j < n; j++)
		if (s[j] == 0 && distance[j] < minDis)
		{
			u = j;
			minDis = distance[j];
		}
	//当已不再存在路径时,算法结束。此语句对非连通图是必需的
	if (minDis == MaxWeight) return;
	s[u] = 1; //标记顶点u已从集合T加入到集合S中
	//修改从v0到其他顶点的最短距离和最短路径
	for (j = 0; j < n; j++)
		if (s[j] == 0 && G.edge[u][j] < MaxWeight && distance[u] + G.edge[u][j] < distance[j])
		{
			//顶点v0经顶点u到其他顶点的最短距离和最短路径
			distance[j] = distance[u] + G.edge[u][j];
			path[j] = u;
		}
}

}
void dispath(AdjMGraph g, int dist[], int path[], int s[], int v)
{
int i = 0, j, k, n = g.Vertices.size;
int apath[100], d;
for (i = 0; i < n; i++)
{

	if ( i != v)
	{
		printf("从顶点%d到顶点%d的最短路径距离为%d", v, i, dist[i]);
		d = 0;
		apath[d] = i;
		k = path[i];
		if (k == -1)
			printf("无路径\n");
		else
		{
			while (k != v)
			{
				d++;
				apath[d] = k;
				k = path[k];
			}
			d++;
			apath[d] = v;
			printf("\n最短路径为:");
			printf("\n%d ", apath[d]);
			for (j = d - 1; j >= 0; j--)
				printf("%d ", apath[j]);
			printf("\n");
		}
	}
}

}

SeqList.h
typedef struct
{
DataType list[MaxSize];
int size;
}SeqList;
void ListInitiate(SeqList *L)
{
L->size=0;
}
int ListInsert(SeqList *L,int i,DataType x)
{
int j;
if(L->size >= MaxSize)
{
printf(“顺序表已满无法插入!\n”);
return 0;
}
else if(i<0 || i > L->size)
{
printf(“参数i不合法!\n”);
return 0;
}
else
{
for(j=L->size;j>i;j–) L->list[j]=L->list[j-1];
L->list[i]=x;
L->size ++;
return 1;
}
}

AdjMGraphCreate.h
typedef struct
{
int row;
int col;
int weight;
}RowColWeight;
void CreatGraph(AdjMGraph *G,DataType V[],int n,RowColWeight E[],int e)
{
int i,k;
Initiate(G,n);
for(i=0;i InsertVertex(G,V[i]);
for(k=0;k InsertEdge(G,E[k].row,E[k].col,E[k].weight);
}

AdjMGraph.h
#include"SeqList.h"
typedef struct
{
SeqList Vertices;
int edge[MaxVertices][MaxVertices];
int numOfEdges;
}
AdjMGraph;
void Initiate(AdjMGraph *G,int n)
{
int i,j;
for(i=0;i for(j=0;j {
if(i==j) G->edge[i][j]=0;
else G->edge[i][j]=MaxWeight;
}
G->numOfEdges=0;
ListInitiate(&G->Vertices);
}
void InsertVertex(AdjMGraph *G,DataType vertex)
{
ListInsert(&G->Vertices,G->Vertices.size,vertex);
}
void InsertEdge(AdjMGraph *G,int v1,int v2,int weight)
{
if(v1 < 0 || v1>=G->Vertices.size || v2<0 || v2>=G->Vertices.size)
{
printf(“参数v1或v2越界错误!\n”);
return;
}
G->edge[v1][v2]=weight;
G->numOfEdges++;
}
int GetFirstVex(AdjMGraph G,int v)
{
int col;
if(v<0 || v>=G.Vertices.size)
{
printf(“参数v1越界出错!\n”);
return -1;
}
for(col=0;col if(G.edge[v][col] > 0 && G.edge[v][col] < MaxWeight)
return col;
return -1;
}
int GetNextVex(AdjMGraph G,int v1, int v2)
{
int col;
if(v1 < 0 || v1 >= G.Vertices.size || v2 < 0 || v2 >= G.Vertices.size)
{
printf(“参数v1或v2越界错误!\n”);
return -1;
}
for(col = v2+1;col < G.Vertices.size; col++)
if(G.edge[v1][col] > 0 && G.edge[v1][col] < MaxWeight)
return col;
return -1;
}

#include
#include
typedef char DataType;
#define MaxSize 50
#define MaxVertices 50
#define MaxWeight 10000
#include"AdjMGraph.h"
#include"AdjMGraphCreate.h"
#include"Dijkstra.h"
int main()
{
AdjMGraph g;
char a[] = { ‘A’,‘B’,‘C’,‘D’,‘E’,‘F’,‘G’,‘H’,‘I’,‘J’,‘K’,‘L’,‘M’,‘N’,‘O’ };
RowColWeight rcw[] = { {0,1,200},{1,0,200},{0,2,300},{2,0,300},{1,3,150},{3,1,150},{2,3,250},{3,2,250},{1,4,150},{4,1,150},{3,4,50},{4,3,50},{4,5,40},{5,4,40},{3,10,300},{10,3,300},{2,10,350},{10,2,350},{10,9,40},{9,10,40},{2,8,200},{8,2,200},{8,6,100},{6,8,100},{2,6,250},{6,2,250},{6,7,30},{7,6,30},{7,11,70},{11,7,70},{11,12,500},{12,11,500},{12,13,15},{13,12,15},{13,14,70},{14,13,70} };
printf(“江西农业大学地点列表:\n”);
printf(“0:江西农业大学南昌商学院(东南门)\n”);
printf(“1:江西省动物生物技术重点开放实验室\n”);
printf(“2:外语楼\n”);
printf(“3:教学大楼\n”);
printf(“4:停车场P\n”);
printf(“5:图书馆\n”);
printf(“6:学院浴室\n”);
printf(“7:学生食堂\n”);
printf(“8:江西农人园林景观设计有限公司\n”);
printf(“9:运动场\n”);
printf(“10:二门诊\n”);
printf(“11:ATM取款机位置\n”);
printf(“12:学生宿舍12栋\n”);
printf(“13:学生宿舍13栋\n”);
printf(“14:江西农业大学南昌商学院(北门)\n”);
int y,n = 15, e = 36;
int distance[15], path[36], s[15];
CreatGraph(&g, a, n, rcw, e);
printf(“输入你所在的起点位置:”);
scanf_s("%d", &y);
Dijkstra(g, y, distance, path);
dispath(g,distance,path,s,y );
system(“pause”);
}

你可能感兴趣的:(数据结构)