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;
    }
    

      运行结果

  • 相关阅读:
    Vue.js 初尝试
    docker 搭建lnmp开发环境
    【转】【Salesfoece】在 Apex 中得到 sObject 的信息
    【转】【Salesfoece】Approval Process 在 Apex 中的使用
    【转】【Salesfoece】Apex 中 PageReference 的使用
    「codeforces
    「二次剩余」Tonelli
    「loj
    pytest---mock使用(pytest-mock)
    Django---setting文件详解
  • 原文地址:https://www.cnblogs.com/ygsworld/p/10023761.html
Copyright © 2011-2022 走看看