邻接矩阵表示有向带权图
#include
#include
#include
typedef char VertexType[5]; //存储顶点值
#define MaxSize 50
#define INIT 10000
typedef struct //邻接矩阵,存储弧的信息
{
int adj;
}ArcNode,AdjMatrix[MaxSize][MaxSize];
typedef struct //图的类型定义
{
VertexType vex[MaxSize]; //存储顶点值
AdjMatrix arc; //邻接矩阵
int arcnum,vexnum; //前者弧数,后者顶点数
}MGraph;
void CreateVertex(MGraph *G) //创建邻接矩阵
{
int i,j,k,w;
VertexType v1,v2;
printf("请输入有向带权图的顶点数和弧数:(空格间隔)\n");
scanf("%d%d",&(*G).vexnum,&(*G).arcnum);
printf("请输入%d个顶点的值:\n",G->vexnum);
for ( i=0 ; ivexnum ; i++ )
{
scanf("%s",&G->vex[i]);
}
for ( i=0 ; ivexnum ; i++ ) //初始化邻接矩阵
{
for ( j=0 ; jvexnum ; j++ )
{
G->arc[i][j].adj = INIT;
}
}
printf("请输入%d条弧的弧尾,弧头和权值:\n",G->arcnum);
for ( k=0 ; karcnum ; k++ )
{
scanf("%s%s%d",v1,v2,&w);
i = LocateVertex(*G,v1);
j = LocateVertex(*G,v2);
G->arc[i][j].adj = w;
}
}
int LocateVertex(MGraph G,VertexType v) //索引
{
int i;
for ( i=0 ; i
分享题目:邻接矩阵表示有向带权图
网页URL:http://ybzwz.com/article/pgshoj.html