Shortest-circuit---dijsktra--adjacency matrix

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.