zoukankan      html  css  js  c++  java
  • 图的邻接矩阵存储

    /*******************************
    *	
    *	Author: Wang Yong
    *	Blog: http://www.cnblogs.com/newwy
    *
    ********************************/
    #include <stdio.h>
    
    #define MAXV  100
    //定义最大顶点个数
    
    //邻接矩阵的数据类型
    typedef char ElemType;
    typedef struct
    {
    	int no;			//顶点编号 
    	ElemType info;	//顶点其他信息 
    }VertexType;		//顶点类型
    
    typedef struct		//图的定义 
    {
    	int edges[MAXV][MAXV];		//邻接矩阵
    	int n,e;					//分别为顶点数和边数
    	VertexType vexs[MAXV];		//存放顶点信息 
    }MGraph;
    
    void CreateMat(MGraph &g,int A[][MAXV],int n)//创建图的邻接矩阵;
    {
    	int i,j;
    	g.n = n;
    	g.e = 0;
    	for(i = 0 ; i < n; i++)
    		for(j = 0 ; j < n ; j++)
    		{
    			g.edges[i][j] = A[i][j];
    			if(g.edges[i][j] != 0)
    				g.e++;			//边数+1 
    		}
    }
    
    void DisMat(MGraph g)
    {
    	int i,j;
    	for(i = 0 ; i < g.n;i++)
    	{
    		for(j = 0 ; j < g.n;j++)
    			printf("%4d",g.edges[i][j]);
    		printf("\n");
    	}
    }
    int main()
    {
    	MGraph g;
    	printf("请输入顶点的数目:");
    	int n;
    	scanf("%d",&n);
    	int A[100][100];
    	int i,j;
    	for(i = 0; i < n;i++)
    		for(j = 0 ; j < n;j++)
    			scanf("%d",&A[i][j]);
    	CreateMat(g,A,n);
    	DisMat(g);
    } 
    
  • 相关阅读:
    bzoj2438
    bzoj3040
    [AHOI2009]维护序列
    [JSOI2008]最大数
    洛谷3378堆模板
    洛谷1439
    洛谷2756
    bzoj1016
    洛谷1875
    [模板] 强连通分量
  • 原文地址:https://www.cnblogs.com/newwy/p/1865314.html
Copyright © 2011-2022 走看看