1 #include "stdafx.h" 2 #include <iostream> 3 4 using namespace std; 5 6 typedef char VertexType; 7 typedef int EdgeType; 8 const int MAXVEX = 100; 9 const int INFINITY = 65535; 10 11 class MGraph 12 { 13 public: 14 VertexType vex[MAXVEX];//顶点表 15 EdgeType arc[MAXVEX][MAXVEX];//邻接矩阵,可看做边表 16 int numVertexes,numEdges;//图中当前的顶点数和边数 17 }; 18 19 20 //创建无向网图 21 void CreateMGraph(MGraph &G) 22 { 23 int i,j,k,w; 24 cout<<"输入顶点数和边数"<<endl; 25 cin>>G.numVertexes>>G.numEdges; 26 for(i = 0;i!=G.numVertexes;i++) 27 cin>>G.vex[i]; 28 for(i = 0;i!=G.numVertexes;i++) 29 for(j = 0;j<G.numVertexes;j++) 30 G.arc[i][j]=INFINITY; 31 for(k = 0;k!=G.numEdges;k++) 32 { 33 cout<<"输入边(vi,vj)上的下标i和j,以及权w"<<endl; 34 cin>>i>>j>>w; 35 G.arc[i][j]=w; 36 G.arc[j][i]=G.arc[i][j]; 37 } 38 } 39 40 41 int _tmain(int argc, _TCHAR* argv[]) 42 { 43 return 0 ; 44 }
原文:http://www.cnblogs.com/crazycodehzp/p/3583711.html