数据结构:图的基本操作

#include
#include
#include
#define MaxVertexNum 25
#define INF 32767
typedef int EdgeType;
typedef struct
{
	char citys[MaxVertexNum][10];
	EdgeType edges[MaxVertexNum][MaxVertexNum];
	int n,e;

}MGraph;
int CityID(MGraph *G,char CityName[])
{
	int i;
	for(i=0;in;i++)
	{
		if(strcmp(CityName,G->citys[i])==0)
			break;
	}
	if(i==G->n)
		return -1;
	else 
		return i;
}
void CreatMGraph(MGraph *G)
{
	int i,j,k,l;
	char t[10],m[10];
	printf("\n请输入城市信息,输入q退出:\n");
	for(i=0;;i++)
	{
		printf("\n城市名称%d:",i);
		scanf("%s",t);
		if(strcmp(t,"q")==0)
			break;
		if(CityID(G,t)>=0)
		{
			printf("\n已有此城市!\n");
			i--;
			continue;
		}
		strcpy(G->citys[i],t);
		G->n=i+1;
	}
	printf("\n请输入公路的信息,输入q退出:\n");
	for(i=0;;i++)
	{

		printf("\n起始城市:");
		scanf("%s",t);
		if(strcmp(t,"q")==0)
			break;
		printf("终止城市:");
		scanf("%s",m);
		if(strcmp(m,"q")==0)
			break;
		printf("公路长度:");
		scanf("%d",&l);
		if(strcmp(t,"q")==0)
		break;
		j=CityID(G,t);
		k=CityID(G,m);
		if(-1==j||-1==k)
		{
			printf("\n城市名称输入有误!\n");
			continue;
		}
		G->edges[j][k]=1;
		G->edges[k][j]=1;
	}
	G->e=i;
}
typedef struct
{ 
	int adjvex;
	int lowcost;
}Closedge;
void Prim(MGraph*G,int v)
{
	int k,i,j,minCost;
	Closedge closedge[MaxVertexNum];
	closedge[v].lowcost=0;
	for(j=0;jn;j++)
	{
		closedge[j].adjvex=v;
		closedge[j].lowcost=G->edges[v][j];
	}
	for(i=1;in;i++)
	{
		for(j=0;jn;j++)
		if(closedge[j].lowcost!=0)
		{
			k=j;
			break;
		}
		minCost=closedge[k].lowcost;
		for(j=0;jn;j++)
		if(closedge[j].lowcostcitys[closedge[k].adjvex],
			G->citys[k]);
		closedge[k].lowcost=0;
		for(j=0;jn;j++)
		{
			if(G->edges[k][j]edges[k][j];
			}
		}
		getchar();
	}
}
	void dispath(MGraph*G,int dist[],int path[],int s[],int v)
	{
		int i,k;
		for(i=0;in;i++)
			if(s[i]==1)
			{k=i;
		printf("\n%s到%s的最短路径为:",G->citys[v],G->citys[i]);
		while(k!=v)
		{printf("%s<-",G->citys[k]);
		k=path[k];
		}
		printf("%s路径长度为:%d\n",G->citys[v],dist[i]);
		}
			else
				printf("%s<-%s不存在路径\n",G->citys[i],G->citys[v]);
	}
	void dijkstra(MGraph*G,int v)
	{
		int dist[MaxVertexNum],path[MaxVertexNum];
		int s[MaxVertexNum];
		int mindis;
		int i,j,k;
		for(i=0;in;i++)
		{
			dist[i]=G->edges[v][i];
			s[i]=0;
			if(G->edges[v][i]<32767)
				path[i]=v;
			else 
				path[i]=-1;
		}
		s[v]=1;
		for(i=0;in;i++)
		{
			mindis=INF;
			k=v;
			for(j=0;jn;j++)
			{
				if(s[j]==0&&dist[j]n;j++)
			{
				if(s[j]==0&&G->edges[k][j]<32767&&dist[k]+G->edges[k][j]edges[k][j];
					path[j]=k;
				}
			}
		}
		dispath(G,dist,path,s,v);
	}
	void consult(MGraph*G)
	{
		char c[10];
		int i;
		printf("\n请输入出发城市:");
		scanf("%s",c);
		i=CityID(G,c);
		if(-1==i)
		{
			printf("\n找不到城市!");
			getchar();
			return;
		}
		dijkstra(G,i);
		getchar();
	}
	int menu()
	{
		int n;
		char c;
		printf("\n\n\t\t城市公路交通系统:\n");
		printf("\n\t\t\t1建立城市公路交通图");
		printf("\n\t\t\t2光纤铺设解决方案");
		printf("\n\t\t\t3交通咨询");
		printf("\n\t\t\t0退出");
		do
		{
			fflush(stdin);
			printf("\n\n\t\t请输入0~3选择功能:");
			c=getchar();
			n=c-48;
			if(n<0||n>3)
				printf("\t\t\t输入选项错误!请重新输入选项");
		}while(n<0||n>3);
		return n;
	}
	int main()
	{
		int select,i,j;
		char c[10];
		MGraph*G;
		G=(MGraph*)malloc(sizeof(MGraph));
		G->n=0;
		G->e=0;
			for(i=0;iedges[i][j]=INF;
				else

					G->edges[i][j]=0;
		do
		{
			system("cls");
			select=menu();
			switch(select)
			{
			case 1:CreatMGraph(G);break;
			case 2:Prim(G,0);break;
			case 3:consult(G);break;
			case 0:printf("\n\n\n\t\t\t谢谢使用!再见。。。\n");
				getchar();
				exit(0);

			}
			getchar();
		}while(select!=0);
	}

			




 

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