zoukankan      html  css  js  c++  java
  • 图(邻接矩阵)

    最新版的请看本博客图的三种存储方式

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <conio.h>
    #include <limits.h>
    
    #define MAXN 256
    #define INFINITY INT_MAX
    #define MAX_VERTEX_NUM 20
    #define OK 1
    
    typedef int VRType;
    typedef int InfoType;
    typedef int VertexType;
    typedef int Status;
    
    typedef VRType AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
    typedef VRType ArcCell;
    typedef int GraphKind;
    
    typedef struct{
        VertexType vexs[MAX_VERTEX_NUM];
        AdjMatrix arcs;
        int vexnum, arcnum;
        GraphKind kind;
    }MGraph;
    
    
    int LocateVex(MGraph *G, VertexType v){
        int i;
        for(i=0; i<G->vexnum; i++)
            if(G->vexs[i] == v) return i;
    }
    
    Status CreateUDN(MGraph *G){
        int i,j,k,v1,v2,w;
        scanf("%d %d", &G->vexnum, &G->arcnum);
        for(i=0; i<G->vexnum; i++) scanf("%d", &G->vexs[i]);
        for(i=0; i<G->vexnum; i++)
            for(j=0; j<G->vexnum; j++) G->arcs[i][j] = INFINITY;
        for(k=0; k<G->arcnum; k++){
            scanf("%d %d %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;
    }
    
    void print(MGraph *G){
        int i,j;
        for(i=0; i<G->vexnum; i++){
            for(j=0; j<G->vexnum; j++){
                if(G->arcs[i][j] == INFINITY) printf("∞\t");
                else printf("%d\t", G->arcs[i][j]);
            }
            putchar('\n');
        }
    }
    
    int main(){
        freopen("d:\\my.txt", "r", stdin);
        MGraph G;
        CreateUDN(&G);
        print(&G);
    
        return 0;
    }
  • 相关阅读:
    Stm32高级定时器(一)
    AES算法简介
    vsim仿真VHDL输出fsdb格式文件
    ncsim仿真VHDL
    云贵高原骑行
    触发器(笔记)
    几种常见的十进制代码(笔记)
    时序电路分类
    组合逻辑电路和时序逻辑电路比较
    数字电路基础(网络整理)
  • 原文地址:https://www.cnblogs.com/tanhehe/p/2883511.html
Copyright © 2011-2022 走看看