1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define MaxVertexNodeNumSize 1000 6 #define MaxVertexNodeNameSize 100 7 8 struct VertexNode 9 { 10 char VertexName[MaxVertexNodeNameSize]; 11 int VertexWeight; 12 }; 13 14 struct _Graph 15 { 16 struct VertexNode VertexList[MaxVertexNodeNumSize]; 17 int ArcList[MaxVertexNodeNumSize][MaxVertexNodeNumSize]; 18 int ArcNum,VertexNum; 19 }; 20 21 //if doesn't find index,return -1 22 int VertexName2Index(struct _Graph *UnsignedGraph,char *VName) 23 { 24 int i; 25 for(i = 0; i < UnsignedGraph -> VertexNum; i ++) 26 { 27 if(strcmp(UnsignedGraph -> VertexList[i].VertexName,VName)==0) 28 { 29 return i; 30 } 31 } 32 return -1; 33 } 34 35 struct _Graph *UGCreat(int ArcSum,int VertexSum) 36 { 37 int i,j; 38 struct _Graph *UnsignedGraph = malloc(sizeof(struct _Graph)); 39 UnsignedGraph -> ArcNum = ArcSum; 40 UnsignedGraph -> VertexNum = VertexSum; 41 42 for(i = 0; i < MaxVertexNodeNumSize; i ++) 43 { 44 for(j = 0; j < MaxVertexNodeNumSize; j ++) 45 { 46 UnsignedGraph -> ArcList[i][j] = INT_MAX; 47 } 48 } 49 50 for(i = 0; i < VertexSum; i ++) 51 { 52 scanf("%s %d",UnsignedGraph -> VertexList[i].VertexName,&UnsignedGraph -> VertexList[i].VertexWeight); 53 } 54 55 for(i = 0; i < ArcSum; i ++) 56 { 57 char Arc_1[MaxVertexNodeNameSize]; 58 char Arc_2[MaxVertexNodeNameSize]; 59 int ArcIndex_1; 60 int ArcIndex_2; 61 int ArcWeight; 62 63 scanf("%s %s %d",Arc_1,Arc_2,&ArcWeight); 64 65 ArcIndex_1 = VertexName2Index(UnsignedGraph,Arc_1); 66 ArcIndex_2 = VertexName2Index(UnsignedGraph,Arc_2); 67 UnsignedGraph -> ArcList[ArcIndex_1][ArcIndex_2] = UnsignedGraph -> ArcList[ArcIndex_2][ArcIndex_1] = ArcWeight; 68 } 69 return UnsignedGraph; 70 } 71 72 void Travel(struct _Graph *UnsignedGraph) 73 { 74 char StartingPoint[MaxVertexNodeNameSize]; 75 char OverPoint[MaxVertexNodeNameSize]; 76 77 printf("Input start and over "); 78 scanf("%s %s",StartingPoint,OverPoint); 79 80 int StartIndex = VertexName2Index(UnsignedGraph,StartingPoint); 81 int OverIndex = VertexName2Index(UnsignedGraph,OverPoint); 82 83 printf("Distance:%d GetVertexPointSum:%d",UnsignedGraph->ArcList[StartIndex][OverIndex] 84 ,UnsignedGraph->VertexList[StartIndex].VertexWeight+UnsignedGraph->VertexList[OverIndex].VertexWeight); 85 } 86 87 int main() 88 { 89 struct _Graph *G = UGCreat(8,5); 90 91 Travel(G); 92 return 0; 93 } 94 95 /* 96 beijing 18 97 zhengzhou 10 98 hefei 9 99 nanjing 12 100 guangzhou 14 101 beijing zhengzhou 7 102 beijing hefei 9 103 beijing nanjing 8 104 zhengzhou hefei 5 105 hefei nanjing 3 106 zhengzhou guangzhou 7 107 hefei guangzhou 8 108 nanjing guangzhou 6 109 110 */