zoukankan      html  css  js  c++  java
  • 写了个无向图邻接矩阵创建过程(最基本的)

    #include<iostream>
    using namespace std;
    typedef char VertexType;
    typedef int EdgeType;
    #define MAXVEX 100
    #define INFINITY 65535
    typedef struct 
    {
            VertexType vexs[MAXVEX];
            EdgeType arc[MAXVEX][MAXVEX];
            int numVertexes,numEdges;
            }MGraph;
    
    int main()
    {
         MGraph *G=new MGraph;
         int i,j,k,w;
         cout<<"输入定点数和边数:"<<endl;
         cin>>G->numVertexes>>G->numEdges;
         for(i=0;i<G->numVertexes;++i)
         cin>>G->vexs[i];
         
         for(i=0;i<G->numVertexes;++i)
              for(j=0;j<G->numVertexes;++j)
                 G->arc[i][j]=INFINITY; //邻接矩阵初始化
                 
         for(k=0;k<G->numEdges;++k) 
         {
              cout<<"输入边(vi,vj)上的下标i、下标j和权值w:"<<endl;
              cin>>i>>j>>w;
              cout<<i<<j<<w<<endl; 
              G->arc[i][j]=w;
              G->arc[j][i]=G->arc[i][j];//由于是无向图,矩阵对称 
         }
         for(i=0;i<G->numVertexes;++i)
             for(j=0;j<G->numVertexes;++j)
                 {cout<<G->arc[i][j];}
         system("pause");
      return 0;                         
    }

    起初总出现问题,由于我输入顶点的时候,忽略了char类型,而输入的是v1 v2 v3 v4,这样就导致程序执行中的崩溃,和逻辑混乱。

  • 相关阅读:
    字符串方法
    文件上传路径转虚拟路径
    表结构转excel
    @ModelAttribute
    select
    查询详情在模态框展示
    时间
    mybatis一对多
    bootstrap tab页
    为什么不建议使用WordPress呢?
  • 原文地址:https://www.cnblogs.com/lx09110718/p/2732156.html
Copyright © 2011-2022 走看看