标签:
邻接矩阵:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 typedef char VertexType[4]; 8 typedef int VRtype; 9 #define INF 100000 10 #define Max 20 11 12 typedef struct /*定点*/ 13 { 14 VRtype adj; /*权值*/ 15 }ARCNode,AdjMatrix[Max][Max]; 16 typedef struct /*图类型*/ 17 { 18 VertexType vex[Max]; /*储存定点*/ 19 AdjMatrix arc; /*邻接矩阵*/ 20 int vexnum,arcnum; /*定点,边数目*/ 21 }Mgraph; 22 23 void CreateGraph(Mgraph *N); 24 void Display(Mgraph N); 25 int Locate(Mgraph *N,char *v); 26 27 int main() 28 { 29 Mgraph N; 30 CreateGraph(&N); 31 Display(N); 32 } 33 int Locate(Mgraph *N,char *v) 34 { 35 int i; 36 for(i=0;i<N->vexnum;i++) 37 if(strcmp(v,N->vex[i])==0) 38 return i; 39 return -1; 40 } 41 void CreateGraph(Mgraph *N) 42 { 43 44 VertexType v1,v2; 45 VRtype w; 46 cout<<"输入定点,边数目"<<endl; 47 cin>>N->vexnum>>N->arcnum; 48 int i,j,k; 49 cout<<"输入定点名称"<<endl; 50 for(i=0;i<N->vexnum;i++) /*输入定点名称*/ 51 cin>>N->vex[i]; 52 53 for(i=0;i<N->vexnum;i++) /*初始化*/ 54 { 55 for(j=0;j<N->vexnum;j++) 56 { 57 N->arc[i][j].adj=INF; 58 } 59 } 60 cout<<"输入边权值"<<endl; 61 for(k=0;k<N->arcnum;k++) 62 { 63 cin>>v1>>v2>>w; 64 i=Locate(N,v1); 65 j=Locate(N,v2); 66 N->arc[i][j].adj=w; 67 } 68 } 69 void Display(Mgraph N) 70 { 71 int i,j,k; 72 for(i=0;i<N.vexnum;i++) 73 { 74 for(j=0;j<N.vexnum;j++) 75 { 76 cout<<N.arc[i][j].adj<<" "; 77 } 78 cout<<endl; 79 } 80 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/4810923.html