输入:
请输入顶点数及弧数
请按照(顶点,顶点,权值)格式输入各边依附的顶点及权值
输出:
图的结构如下,用邻接矩阵输出
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define INFINITY INT_MAX //最大值
#define MAX_VERTEX_NUM 20 //最大顶点个数
#define FALSE 0
#define TRUE 1
#define OK 1
#define ERROR -2
#define OVERFLOW -1
typedef struct { // 图的定义
int vexnum, arcnum; // 顶点数,弧数
int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
} MGraph;
MGraph CreateUDN(MGraph G)
{
int i,j,k;
printf("请输入顶点数及弧数:\n");
scanf("%d,%d",&G.vexnum,&G.arcnum);
for(i=0;i<G.vexnum;++i)
for(j=0;j<G.vexnum;++j)
G.AdjMatrix[i][j]=INFINITY;
printf("请按照(顶点,顶点,权值)格式输入各边依附的顶点及权值:\n");
for(k=0;k<G.arcnum;++k)
{ int i,j,w;
scanf("%d,%d,%d",&i,&j,&w);
G.AdjMatrix[i][j]=w;
G.AdjMatrix[j][i]=G.AdjMatrix[i][j];}
return G;
}
/*void OutputGraph(MGraph G)
{
int i,j;
printf("图的结构如下,按照(顶点,顶点,权值)格式输出\n");
for(i=0;i<G.vexnum;++i)
for(j=0;j<G.vexnum;++j)
if(G.AdjMatrix[i][j]<INFINITY)
printf("%d,%d,%d\n",i,j,G.AdjMatrix[i][j]);
}*/
void OutputGraph(MGraph G)
{
int i,j;
printf("图的结构如下,用邻接矩阵输出\n");
for(i=0;i<G.vexnum;++i)
{for(j=0;j<G.vexnum;++j)
printf("%d ",G.AdjMatrix[i][j]);
printf("\n");}
}
void main()
{ MGraph G;
G=CreateUDN(G);
OutputGraph(G);
}