Dijkstra algorithm Templates

Source: Internet
Author: User

#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intinf=1<< in;Const intn=1001;intN,m,d[n],w[n][n];BOOLVis[n];voidDij () { for(intI=1; i<=n; i++)//The distance from the starting point to the rest of each point is set to infinity, which facilitates the relaxation operation thereafter.d[i]=inf; d[1]=0;//starting with 1     for(intI=0; i<n; i++)//Make sure no slack is omitted, loop n times (because each value is iterated through only the current minimum distance from the origin) .    {        intnow=inf; intx;  for(intj=1; j<=n; J + +)//from front to back, the distance from the starting point to each point is taken out more than once (each cycle, the distance will be updated)        {            if(!vis[j]&&now>d[j])//vis avoids repeated comparisons of points that have previously been compared to the minimum values            {                //always find the point closest to the starting point, based on this point, down to find other points to that point distance,now=D[j]; X=J; }        }       //printf ("x=%d\n", x);vis[x]=1;  for(intj=1; j<=n; J + +)//from X point to all other points, all traverse, the update (slack) operation of the distance ,        {            //! VIS[J] Avoid repeating updates with the previous minimum points            if(!vis[j]&&d[j]>d[x]+w[x][j])//if (this point to X distance plus x to start distance) is less than the distance from the previous point to the starting point{D[j]=D[X]+W[X][J];//is updated to a new distance//printf ("d[%d]=%d\n", J,d[j]);            }        }    }}intMain () { while(~SCANF ("%d%d", &n,&m))//points, number of sides{memset (Vis,0,sizeof(VIS));  for(intI=1; i<=n; i++)        {             for(intj=1; j<=n; J + +) {W[i][j]=inf;//the distance from point I to J is all initialized to infinity            }        }         while(m--)        {            intU,v,c; scanf ("%d%d%d", &u,&v,&c);//starting point, end point, weight value            if(c<W[u][v]) W[u][v]=w[v][u]=c;//the weighted value of the no-map pay} dij ();  for(intI=2; i<=n; i++)//all outputs the minimum value of each point to the starting point{printf ("%d%d\n", I,d[i]); }    }    return 0;}/*INPUT7 121 2 241 3 Bayi 4 5 5 3output2 6 7 7 5 7 4 2 173 155 136*/

Dijkstra algorithm Templates

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.