最短路径(Dijkstra实现)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <stdbool.h>
  5 
  6 #define MaxVertexNodeNumSize 1000
  7 #define MaxVertexNodeNameSize 100
  8 
  9 struct VertexBodyNode
 10 {
 11     char VertexName[MaxVertexNodeNameSize];
 12     int ArcWeight;
 13     int VertexIndex;
 14     struct VertexBodyNode *Next;
 15 };
 16 
 17 struct VertexHeadNode
 18 {
 19     char VertexName[MaxVertexNodeNameSize];
 20     int VertexWeight;
 21     struct VertexBodyNode *Next;
 22 };
 23 
 24 struct _Graph
 25 {
 26     struct VertexHeadNode VertexHeadNodeList[MaxVertexNodeNumSize];
 27     int ArcNum,VertexNum;
 28 };
 29 
 30 int VertexName2Index(struct _Graph *DirectedGraph,char *VName)
 31 {
 32     int i;
 33     for(i = 0; i < DirectedGraph -> VertexNum; i ++)
 34     {
 35         if(strcmp(DirectedGraph -> VertexHeadNodeList[i].VertexName,VName)==0)
 36         {
 37             return i;
 38         }
 39     }
 40     return -1;
 41 }
 42 
 43 void AddOneArc(struct _Graph *DirectedGraph,int ArcIndex_1,int ArcIndex_2,int AWeight)
 44 {
 45     struct VertexBodyNode *BNode = malloc(sizeof(struct VertexBodyNode));
 46 
 47     strcpy(BNode -> VertexName,DirectedGraph -> VertexHeadNodeList[ArcIndex_2].VertexName);
 48 
 49     BNode -> ArcWeight = AWeight;
 50     BNode -> VertexIndex = ArcIndex_2;
 51     BNode -> Next = NULL;
 52 
 53     struct VertexBodyNode *TmpPointer;
 54     TmpPointer = DirectedGraph -> VertexHeadNodeList[ArcIndex_1].Next;
 55     while(TmpPointer != NULL && TmpPointer -> Next != NULL)
 56     {
 57         TmpPointer = TmpPointer -> Next;
 58     }
 59     if(TmpPointer==NULL)
 60     {
 61         DirectedGraph -> VertexHeadNodeList[ArcIndex_1].Next = BNode;
 62     }
 63     else
 64     {
 65         TmpPointer -> Next = BNode;
 66     }
 67 }
 68 
 69 struct _Graph *UGCreat(int ArcSum,int VertexSum)
 70 {
 71     int i,j;
 72     struct _Graph *DirectedGraph = malloc(sizeof(struct _Graph));
 73     DirectedGraph -> ArcNum = ArcSum;
 74     DirectedGraph -> VertexNum = VertexSum;
 75 
 76     for(i = 0; i < VertexSum; i ++)
 77     {
 78         scanf("%s %d",DirectedGraph -> VertexHeadNodeList[i].VertexName,&DirectedGraph -> VertexHeadNodeList[i].VertexWeight);
 79     }
 80 
 81     for(i = 0; i < VertexSum; i ++)
 82     {
 83         DirectedGraph -> VertexHeadNodeList[i].Next = NULL;
 84     }
 85 
 86     for(i = 0; i < ArcSum; i ++)
 87     {
 88         char Arc_1[MaxVertexNodeNameSize];
 89         char Arc_2[MaxVertexNodeNameSize];
 90         int ArcIndex_1;
 91         int ArcIndex_2;
 92         int ArcWeight;
 93 
 94         scanf("%s %s %d",Arc_1,Arc_2,&ArcWeight);
 95 
 96         ArcIndex_1 = VertexName2Index(DirectedGraph,Arc_1);
 97         ArcIndex_2 = VertexName2Index(DirectedGraph,Arc_2);
 98 
 99         AddOneArc(DirectedGraph,ArcIndex_1,ArcIndex_2,ArcWeight);
100     }
101     return DirectedGraph;
102 }
103 
104 void Travel(struct _Graph *DirectedGraph)
105 {
106     char StartingPoint[MaxVertexNodeNameSize];
107     char OverPoint[MaxVertexNodeNameSize];
108 
109     printf("Input start and over\n");
110     scanf("%s %s",StartingPoint,OverPoint);
111 
112     int StartIndex = VertexName2Index(DirectedGraph,StartingPoint);
113     int OverIndex = VertexName2Index(DirectedGraph,OverPoint);
114 
115     struct VertexBodyNode *TmpPointer;
116     TmpPointer = DirectedGraph -> VertexHeadNodeList[StartIndex].Next;
117     while(TmpPointer != NULL)
118     {
119         if(OverIndex==TmpPointer -> VertexIndex)
120         {
121             printf("Distance:%d GetVertexPointSum:%d",TmpPointer->ArcWeight
122                    ,DirectedGraph -> VertexHeadNodeList[StartIndex].VertexWeight+DirectedGraph -> VertexHeadNodeList[OverIndex].VertexWeight);
123             break;
124         }
125         else
126         {
127             TmpPointer = TmpPointer -> Next;
128         }
129     }
130 }
131 
132 void DijkstraShortestPath(struct _Graph *DirectedGraph,char *Origin,char *Destination)
133 {
134     int Visit[DirectedGraph->VertexNum];
135     int Distance[DirectedGraph->VertexNum];
136     int PathResult[DirectedGraph->VertexNum];
137     int OIndex = VertexName2Index(DirectedGraph,Origin);
138     int DIndex = VertexName2Index(DirectedGraph,Destination);
139     memset(Visit,0,sizeof(Visit));
140     memset(PathResult,0,sizeof(PathResult));
141     int i,j;
142     for(i = 0; i < DirectedGraph->VertexNum; i ++)
143     {
144         Distance[i] = INT_MAX;
145         PathResult[i] = OIndex;
146     }
147 
148     struct VertexBodyNode *TmpPointer;
149     
150     TmpPointer = DirectedGraph -> VertexHeadNodeList[OIndex].Next;
151     while(TmpPointer != NULL)
152     {
153         Distance[TmpPointer->VertexIndex] = TmpPointer -> ArcWeight;
154         TmpPointer = TmpPointer -> Next;
155     }
156     
157     int MinCost;
158     int MinCostIndex = OIndex;
159     for(i = 0;i < DirectedGraph->VertexNum-1;i ++)
160     {
161         MinCost = INT_MAX;
162 
163         for(j = 0;j < DirectedGraph->VertexNum;j ++)
164         {
165             if(Visit[j]==0 && Distance[j]<MinCost)
166             {
167                 MinCostIndex = j;
168                 MinCost = Distance[j];
169             }
170         }
171         
172         Visit[MinCostIndex] = 1;
173         
174         TmpPointer = DirectedGraph -> VertexHeadNodeList[MinCostIndex].Next;
175         while(TmpPointer != NULL)
176         {
177             if(Visit[TmpPointer->VertexIndex]==0 
178             && MinCost+TmpPointer->ArcWeight<Distance[TmpPointer->VertexIndex])
179             {
180                 Distance[TmpPointer->VertexIndex] = MinCost+TmpPointer->ArcWeight;
181                 PathResult[TmpPointer->VertexIndex] = MinCostIndex;
182             }
183             TmpPointer = TmpPointer -> Next;
184         }
185     }
186     
187     int TmpSearchIndex = DIndex;
188     while(TmpSearchIndex != OIndex)
189     {
190         printf("%s <- ",DirectedGraph->VertexHeadNodeList[TmpSearchIndex].VertexName);
191         TmpSearchIndex = PathResult[TmpSearchIndex];
192     }
193     if(DIndex != OIndex)
194         printf("%s     Distance:%d\n",DirectedGraph->VertexHeadNodeList[OIndex].VertexName,Distance[DIndex]);
195     else
196         printf("%s     Distance:%d\n",DirectedGraph->VertexHeadNodeList[OIndex].VertexName,0);
197 }
198 
199 int main()
200 {
201     struct _Graph *G = UGCreat(8,5);
202 
203 //    Travel(G);
204     char Origin[MaxVertexNodeNameSize];
205     char Destination[MaxVertexNodeNameSize];
206     printf("Input Origin && Destination:\n");
207     scanf("%s %s",Origin,Destination);
208     DijkstraShortestPath(G,Origin,Destination);
209     return 0;
210 }
211 
212 /*
213         beijing 18
214         zhengzhou 10
215         hefei 9
216         nanjing 12
217         guangzhou 14
218         beijing zhengzhou 7
219         beijing hefei 9
220         beijing nanjing 8
221         zhengzhou hefei 5
222         hefei nanjing 3
223         zhengzhou guangzhou 7
224         hefei guangzhou 8
225         nanjing guangzhou 6
226 */

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/9427498.html
今日推荐