(ljjz.h)邻接矩阵
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #define FINITY 50000 #define M 20 typedef char vertextype; typedef int edgetype; typedef struct { vertextype vexs[M];//定点信息 edgetype edges[M][M];//邻接矩阵 int n,e;//顶点总数,边总数 }Mgraph; void creat(Mgraph *g,char *s,int c) { int i,j,k,w; FILE *rf;//从文件输入图数据 rf=fopen(s,"r"); if(rf) { fscanf(rf,"%d%d",&g->n,&g->e);//读入顶点数,边数 for(i=0;i <g->n;i++) fscanf(rf,"%1s",&g->vexs[i]);//读入顶点值 for(i=0;i<g->n;i++)//初始化邻接矩阵 for(j=0;j<g->n;j++) if(i==j) g->edges[i][j]=0; else g->edges[i][j]=FINITY; for(k=0;k<g->e;k++)//读入边 { fscanf(rf,"%d%d%d",&i,&j,&w); printf("%d %d %d\n",i,j,w); g->edges[i][j]=w; if(c==0)//如果是无向图则对应j,i相等 g->edges[j][i]=w; } fclose(rf); } else g->n=0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。