33,311
社区成员
发帖
与我相关
我的任务
分享
#include "stdio.h"
#include "malloc.h"
#define MaxVertexNum 100 //图中顶点的最大个数
typedef char VertexType; //顶点的关系类型,对无权图,用1或0表示相邻与否;对带权图,则为权值类型。
typedef int EdgeType; //弧/边上的权值
typedef struct
{
char vertextype;
}Vertex;
typedef struct
{
EdgeType vertype;
}Arc;
typedef struct
{
Vertex vexs[MaxVertexNum];//顶点表
Arc arcs[MaxVertexNum][MaxVertexNum];//邻接矩阵
int n,e;//图中当前的顶点数和弧/边数
}AdjGragh;
//建立无向图的邻接矩阵
void CreateMGraph(AdjGragh *G)
{
int i,j,k,w;
printf("请输入图中当前的顶点数和弧/边数\n");
scanf("%d%d",&G->n,&G->e);
for(i=0;i<G->n;i++)
G->vexs[i].vertextype='A'; //顶点表初始化
for(i=0;i<G->n;i++)
{
for(j=0;j<G->n;j++)
{
G->arcs[i][j].vertype=0;
}
}
//读入e条边,建立邻接矩阵
for(k=0;k<G->e;k++)
{
//无权图
printf("输入边上的Vi,Vj和权w\n");
scanf("%d%d%d",&i,&j,&w); //输入边(Vi,Vj)上的权w
G->arcs[i][j].vertype=w;
G->arcs[j][i].vertype=w;
}
}
void main()
{
AdjGragh *G=(*AdjGragh)malloc(sizeof(AdjGragh));
CreateMGraph(G);
}