1 Const intMAXINT =32767;2 Const intMaxnum =Ten;//Number of points3 intDist[maxnum];4 intPrev[maxnum];5 6 intA[maxunm][maxnum];7 8 voidDijkstra (intv0)9 {Ten BOOLS[maxnum];//Create a new tag array to determine if the point has been stored in the S collection; One intn=Maxnum; A for(intI=1; i<=n; ++i) - { -Dist[i] = A[v0][i];//The adjacency matrix of the point is copied into the dist[] one-dimensional array; theS[i] =false;//the point has not been used initially; - if(Dist[i] = =MAXINT) -Prev[i] =-1; - Else +Prev[i] = V0;//record the point of the previous node is now processed by the source point, if not unicom is-1; - } +Dist[v0] =0;//Dist indicates the shortest distance from the Origin v0 to the point; AS[v0] =true;//mark the source point as having been accessed; at for(intI=2; i<=n; i++)//cycle n-1 times, guaranteed to find every point; - { - intMindist = MAXINT;//marks the current minimum distance, initialized to maximum; - intu = v0;//find the Dist[j] minimum of the point J currently unused - for(intj=1; j<=n; ++j) - if((! S[J]) && dist[j]<mindist) in { -U = j;//U Save the number of the point with the smallest distance in the current adjacency point toMindist =Dist[j]; +}//find out the point closest to the source point, U record the number of points; -S[u] =true; the for(intj=1; j<=n; J + +) * if((! S[J]) && a[u][j]<maxint)//find the point that has not been used, and you are the point of unicom; $ {Panax Notoginseng if(Dist[u] + a[u][j] < Dist[j])//find a shorter path from the v0 point through the newly added U-point path; (if the point is farther from the origin than the distance from the U point, the current shortest path will change) - { theDIST[J] = Dist[u] + a[u][j];//Update Dist +PREV[J] = u;//record precursor vertices A } the } + } -}
View Code
Shortest-circuit---dijsktra--adjacency matrix