代码实现:
//图的邻接矩阵存储结构—用两个数组来表示图,一个一维数组存储图中的顶点信息,一个二维数组(称为邻接数组)存储图中的边或弧的信息。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MAX_VERTICES 100 //假设图有100个顶点
#define MAX_WEIGHT 32767 //加权图(网)不邻接时为10000,但输出为“∞”
typedef struct
{
int Vertices[MAX_VERTICES]; //顶点信息的数组
int AdjacencyMatrix[MAX_VERTICES][MAX_VERTICES]; //边信息的数组。
int numV; //当前的顶点数
int numA; //当前的边数
}AdMatrix;
//图的生成函数
void CreateGraph(AdMatrix *G)
{
int n, e, vi, vj, w;
printf("请输入图的顶点数和边数(空格隔开):");
scanf("%d%d", &n, &e);
G->numV = n;
G->numA = e;
//AdjacencyMatrix初始化
for (int i = 0