zoukankan      html  css  js  c++  java
  • 无向网的邻接矩阵

    #include<iostream>
    using namespace std;
    #define MAX_VERTEX_NUM 20
    
    #define VertexType int 
    
    typedef enum{DG,DN,UDG,UDN}GraphKind;
    
    typedef struct
    {
        VertexType vexs[MAX_VERTEX_NUM];//顶点信息
        int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//矩阵
        int pointnum;//顶点数
        int edgenum;//边数
        GraphKind kind;
    }MGraph;
    
    int Locate(MGraph G,VertexType v)
    {
        for(int i = 0;i<G.pointnum;i++)
        {
            if(v == G.vexs[i])
                return i;
        }
        return -1;
    }
    
    void CreateUDN(MGraph &G)
    {
        int i;
        int j;
        int k;
        int w;
        char flag;  
        VertexType v1,v2;
        G.kind = UDN;
        cout<<"请输入无向网的顶点数,边数"<<endl;
        cin>>G.pointnum>>G.edgenum;
        cout<<"请输入顶点信息"<<endl;
        for(i = 0;i<G.pointnum;i++)
        {
            cin>>G.vexs[i];
            for(j = 0;j<i;j++)
            {
                if(G.vexs[j] == G.vexs[i])
                    break;
            }
            if(j < i)
            {
                i--;
                cout<<"已经存在这样的顶点,重新输入"<<endl;
                continue;
            }
        }
        for(i = 0;i<G.pointnum;i++)
        {
            for( j = 0;j<G.pointnum;j++)
            {
                G.arcs[i][j] = 0;
            }
        }
        for(k = 0;k<G.edgenum;k++)
        {
            cout<<"输入相邻边的顶点及其距离"<<endl;
            cin>>v1>>v2>>w;
            i = Locate(G,v1);
            j = Locate(G,v2);
            if(G.arcs[i][j] == 0)
            {
                G.arcs[i][j] = w;
                G.arcs[j][i] = G.arcs[i][j];
            }
            else
            {
                cout<<"已经存在该边,是否重新输入?Y 确认"<<endl;
                cin>>flag;
                if(flag == 'y'|| flag == 'Y')
                {
                    G.arcs[i][j] = w;
                    G.arcs[j][i] = G.arcs[i][j];
                }
                k--;
            }
        }
    }
    
    void Disp(MGraph G)
    {
        for(int i = 0 ;i<G.pointnum;i++)
            {
                for(int j = 0;j<G.pointnum;j++)
                {
                    cout<<" "<<G.arcs[i][j];
                }
                cout<<endl;
            }
    }
    
    int main()
    {
        MGraph G;
        CreateUDN(G);
        Disp(G);
        return 1;
    }
    

      运行结果

  • 相关阅读:
    MongoDB数据查询详解
    MongoDB增加数据
    laravel安装初体验
    操作MongoDB
    MongoDB基本概念和安装配置
    tp5操作mongo
    c语言运算符优先级与while循环案例
    tp5下通过composer实现日志记录功能
    c语言中类型转换与赋值运算符、算术运算符、关系运算符、逻辑运算符。原码、反码、补码。小解。
    scanf使用与运算符
  • 原文地址:https://www.cnblogs.com/ygsworld/p/10023761.html
Copyright © 2011-2022 走看看