图的遍历 邻接矩阵

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
typedef char VerTexType ;     //顶点
typedef int ArcType;           //弧
using namespace std;
const int maxint =32685;
const int MVNum=100;
typedef struct
{
	VerTexType vexs[MVNum];
	ArcType arcs[MVNum][MVNum];
	int vexnum,arcnum;
}AMGragh;   //Adjacency Matrix Gragh  邻接矩阵
int LocateVex(AMGragh G,VerTexType v)
{
	for(int i=1;i<=G.vexnum;i++)
		if(G.vexs[i]==v)
			return i;
	return -1;
}
void CreatUDN(AMGragh &G)
{
	scanf("%d%d",&G.vexnum,&G.arcnum);
	for(int i=1;i<=G.vexnum;i++)
		cin>>G.vexs[i];
	for(int i=1;i<=G.vexnum;i++)
		for(int j=1;j<=G.vexnum;j++)
			G.arcs[i][j]=maxint;
	VerTexType  v1,v2;
	ArcType w;
	int ii,jj;
	for(int i=1;i<=G.arcnum;i++)
	{
		cin>>v1>>v2>>w;
		ii=LocateVex(G,v1);
		jj=LocateVex(G,v2);
		G.arcs[ii][jj]=w;
		G.arcs[jj][ii]=w;
	}
}
void Print(AMGragh G)
{
	for(int i=1;i<=G.vexnum;i++)
	{
		for(int j=1;j<=G.vexnum;j++)
		{
			
			if(G.arcs[i][j]!=maxint)
			{
				cout<<G.vexs[i]<<G.vexs[j]<<"  "<<G.arcs[i][j];
			}
		}
		cout<<endl;
	}
}
int main()
{
	AMGragh G;
	CreatUDN(G);
	Print(G);
	return 0;
}
posted @ 2020-02-20 12:10  DuJunlong  阅读(6)  评论(0编辑  收藏  举报  来源