无向图的邻接矩阵存储,4个顶点、4条边
作者:互联网
//
// 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 <iostream>
#include <stdio.h>
#include <stdlib.h>
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;i<g->vexnum;i++)
{
getchar();
printf("第%d个顶点:\n",i);
scanf("%c",&ch);
g->vexs[i]=ch;
}
for(i=0;i<g->vexnum;i++)
for(j=0;j<g->vexnum;j++)
g->arcs[i][j]=MAX_INT;
for(k=0;k<g->arcnum;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;i<g->vexnum;i++)
{
for(j=0;j<g->vexnum;j++)
printf("%d ",g->arcs[i][j]);
printf("\n");
}
}
int main(int argc, const char * argv[]) {
WGraph g;
CreateGraph(&g);
printf("输出矩阵:\n");
OutGraph(&g);
}
运行结果如下:
标签:arcs,int,邻接矩阵,++,无向,printf,条边,vnum,WGraph 来源: https://www.cnblogs.com/duanqibo/p/11106815.html