Dijkstra shortest path and its output

Source: Internet
Author: User

High school students let me beg 8 market and 35 points of sale (they will also have 15 intersections) between the 35*8=280 a combination of the shortest path and its output.

The shortest water problem, hey. It's great to have the knowledge you have learned to help others solve problems.

Specific comments can be seen in the code, written pretty clear.

#include <iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>using namespacestd;#defineINF 0x3f3f3f3//inf represents InfinityConst intmax_n= -;intMap[max_n][max_n],vis[max_n];intDis[max_n];//dis represents the shortest distance to each pointintPre[max_n];//Recording precursorintAns[max_n];//an array of output pathsvoidDIJKSTR (intXintm) {//x represents the start point, and M represents the number of vertices     for(intI=1; i<=m;i++) {Dis[i]=Map[x][i]; Vis[i]=0; if(map[x][i]!=INF) Pre[i]=x; ElsePre[i]=-1; }    intp; VIS[X]=1;  for(intI=1; i<=m;i++){           intmin=INF;  for(intj=1; j<=m;j++){               if(!vis[j]&&dis[j]<min) {min=Dis[j]; P=J; }} Vis[p]=1;  for(intj=1; j<=m;j++){            if(!vis[j]&&dis[p]+map[p][j]<Dis[j]) {Dis[j]=dis[p]+Map[p][j]; PRE[J]=p; }                    }               }}intMain () {intM,n,a,b,c; //The number in the input file means: 1-8 is base, 9-43 is point of sale, 44-58 is intersection, format is point distanceFreopen ("In.txt","R", stdin);//read data from a fileFreopen ("OUT.txt","W", stdout);//output results to file    voidDIJKSTR (intXintN); //cout<< "Please enter the number of points and the number of edges" <<endl;Cin>>m>>N; memset (Vis,0,sizeof(VIS));//m points, n edges         for(intI=1; i<=m;i++)         for(intj=1; j<=m;j++) {Map[i][j]=INF; }    //cout<< "Please enter the edges in order of origin, end, distance" <<endl;         for(intj=1; j<=n;j++) {cin>>a>>b>>C; MAP[A][B]=map[b][a]=C; }                for(intI=1; i<=8; i++) {dijkstr (i,m);  for(intj=9; j<= +; j + +){            intk=J; cout<<"from the base"<<i<<"to the point of sale"<< (J-8) <<"the shortest distance is:"<<dis[k]<<Endl; intp,len=1; P=K;  while(p>=1) {Ans[len++]=Q; P=Pre[p]; }       intfirst=0; cout<<"here is the exact path:"<<Endl;  for(intr=len-1; r>=1; r--)       {            if(first++) cout<<" -"; if(ans[r]>=1&&ans[r]<=8) cout<<"Base"<<Ans[r]; Else if(ans[r]>=9&&ans[r]<= +) cout<<"Point of Sale"<<ans[r]-8; Elsecout<<"intersection"<<ans[r]- +; } cout<<Endl; cout<<Endl; }    }        return 0;}

Preview of results in out file:

Detailed data and source code available for download: Http://files.cnblogs.com/files/Tach-ac/%E6%9C%80%E7%9F%AD%E8%B7%AF.rar

Dijkstra shortest path and its output

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.