pat:1030. Travel Plan (AC)

Source: Internet
Author: User

#include<stdio.h>#include<string.h>#include<algorithm>Usingnamespace Std;Constint maxv=510;Constint inf=0X3FFFFFFF;int n,m,s,d;Number of cities, number of highways, starting point, end pointBOOL VISIT[MAXV];Whether the tag has been traversed in the Dijkstraint GD[MAXV][MAXV];Storage Distance Mapint GC[MAXV][MAXV];Storage cost diagramint D[MAXV];Shortest pathint C[MAXV];Lowest costint PRE[MAXV];Recording precursorvoid Dijkstra (int s) {memset (visit,Falsesizeof (visit));Initialize tagsForint i=0; I<n; ++i)Initializes an array of precursors pre[i]=i; Fill (D,d+maxv,inf);Initialize the shortest distance to infinity d[s]=0;The distance from the starting point to the starting point is 0 fill (c,c+maxv,inf);Initialize minimum cost to infinity c[s]=0;Starting point to start point cost is 0Forint i=0; I<n; ++i) {int min=inf,u=-1;Forint j=0; J<n; ++j) {if (d[j]<min && visit[j]==False) {min=d[j]; u=j;}}if (u==-1)Illustration not connectedReturn visit[u]=TrueForint v=0; V<n; ++V) {if (visit[v]==False && Gd[u][v]!=inf)V is not accessed, and u can go to v {if (D[v]>d[u]+gd[u][v])Distance, updated with minimum distance {d[v]=d[u]+gd[u][v]; c[v]=c[u]+gc[u][v]; pre[v]=u;}Elseif (D[v]==d[u]+gd[u][v])The distance is equal, by the lowest cost update {if (C[v]>c[u]+gc[u][v]) {c[v]=c[u]+gc[u][v]; pre[v]=u;}} } } }}void DFS (int S,int v) {if (s==v) {printf ("%d", V);Return } DFS (S,pre[v]); printf"%d", v);}int main () {Fill (gd[0],gd[0]+maxv*maxv,inf); Fill (gc[0],gc[0]+maxv*maxv,inf); scanf "%d%d%d%d" &n, &M,&S,&D); for (int i=0; i<m; ++i) //storage city distance and charge status {int u,v; scanf ( "%d%d" &u,&v); scanf ( "%d%d",& GD[U][V],&GC[U][V]); GD[V][U]=GD[U][V]; GC[V][U]=GC[U][V]; } Dijkstra (S); DFS (S,D); printf ( "%d%d\nreturn 0;}          

pat:1030. Travel Plan (AC)

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: 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.