//
// main.cpp
// Graph_wuxiang
//
// Created by duanqibo on 2019/6/29.
// Copyright © 2019年 duanqibo. All rights reserved.
// 无向图的邻接矩阵存储,4个顶点、4条边,v0到v1:权30;v0到v2:权80;
// v1到v2:权50;v2到v3:权40;其他非邻接点之间用32767表示;
#include
#include
#include
typedef char VertexType;
typedef int WeightType;
const int vnum=4;
const int MAX_INT=32767;
typedef struct gp
{
VertexType vexs[vnum];
WeightType arcs[vnum][vnum];
int vexnum,arcnum;
}WGraph;
void CreateGraph(WGraph *g)
{
int i,j,n,e,w,k;
char ch;
printf("输入顶点数和边数:");
scanf("%d%d",&n,&e);
g->vexnum=n;
g->arcnum=e;
for(i=0;ivexnum;i++)
{
getchar();
printf("第%d个顶点:\n",i);
scanf("%c",&ch);
g->vexs[i]=ch;
}
for(i=0;ivexnum;i++)
for(j=0;jvexnum;j++)
g->arcs[i][j]=MAX_INT;
for(k=0;karcnum;k++)
{
scanf("%d%d%d",&i,&j,&w);
g->arcs[i][j]=w;
g->arcs[j][i]=w;
}
}
void OutGraph(WGraph *g)
{
int i,j;
for(i=0;ivexnum;i++)
{
for(j=0;jvexnum;j++)
printf("%d ",g->arcs[i][j]);
printf("\n");
}
}
int main(int argc, const char * argv[]) {
WGraph g;
CreateGraph(&g);
printf("输出矩阵:\n");
OutGraph(&g);
}
运行结果如下: