当前位置: 代码迷 >> 综合 >> 邻接矩阵(考研图论)
  详细解决方案

邻接矩阵(考研图论)

热度:102   发布时间:2023-11-25 22:55:06.0
//邻接矩阵
#include<stdio.h>
#include<stdlib.h>
#define Status int
#define OVERFLOW -2
#define OK 1
#define ERROR -1
//邻阶矩阵
#define MaxInt 32767
#define MVNum 100
typedef char VerTexType;//顶点的数据类型
typedef int ArcType;//边的权值类型
typedef struct
{VerTexType vexs[MVNum];//顶点表ArcType arcs[MVNum][MVNum];//邻阶矩阵int vexnum,arcnum;//图当前的点数和边数
}AMGraph;
int LocateVex(AMGraph &G,char v)
{for(int i=0;i<G.vexnum;i++){if(v==G.vexs[i])return i;}
}
Status CreateUDN(AMGraph &G)
{char v1,v2;int w;int j;scanf("%d %d",&G.vexnum,&G.arcnum);//输入总顶点数,总边数for(int i=0;i<G.vexnum;++i){scanf(" %c",&G.vexs[i]);//printf("bbb\n");}for(i=0;i<G.vexnum;++i)//初始化邻阶矩阵,边的权值均置为极大值MaxInt{for(j=0;j<G.vexnum;++j){G.arcs[i][j]=MaxInt;//printf("aaa\n");}}for(int k=0;k<G.arcnum;++k)//构造邻阶矩阵{scanf(" %c %c %d",&v1,&v2,&w);i=LocateVex(G,v1);j=LocateVex(G,v2);G.arcs[i][j]=w;G.arcs[j][i]=G.arcs[i][j];}return OK;
}
int main()
{AMGraph G;CreateUDN(G);printf("%d\n",G.arcs[1][2]);//为测试而输出return 0;
}