l2-001. Emergency rescue (Dijkstra algorithm print path)

Source: Internet
Author: User

As the head of a city's emergency rescue team, you have a special national map. The map shows a number of scattered cities and some quick roads connecting the city. The number of rescue teams in each city and the length of each fast road connecting two cities are marked on the map. When other cities have an emergency call for you, your task is to lead your rescue team to the scene as quickly as possible, while gathering as many rescue teams along the way.

Input format:

Enter the first line to give 4 positive integers n, M, S, D, where N (2<=n<=500) is the number of cities, by the way assuming the city number is 0~ (N-1), M is the number of fast roads, S is the city number of the origin, and D is the city number of the destination. The second line gives n positive integers, where number i is the number of rescue teams in City I, separated by a space between the numbers. In the subsequent M-line, each line gives a quick path of information, namely: City 1, City 2, the length of the fast road, the middle with a space separated, the numbers are integers and not more than 500. The input guarantees that rescue is feasible and the optimal solution is unique.

Output format:

The first line outputs the number of different shortest paths and the maximum number of rescue teams that can be convened. The second line outputs the city number that passes through the path from S to D. The numbers are separated by a space, and the output cannot have extra spaces.

Input Sample:

4 5 0 320 30 40 100 1 11 3 20 3 30 2 22 3 2

Sample output:

2 600 1 3

1#include <stdio.h>2#include <math.h>3#include <stdlib.h>4 5 #defineMAX 5026 #defineINFI 99999997 8 intG[MAX][MAX],NV,NE,C1,C2;//Graph, node count, number of sides, start point, end Point9 intKnow[max];//access tokenTen intDistance[max];//Distance One intNum[max];//Shortest Path Bar number A intWeight[max];//weights for each point - intW[max];//sum of point weights on shortest path - intPre[max];//Store a node before each node . the voidPrintpath (intv); - voidcreategraph (); - voidDijkstra (); - intMain () + { -     inti; +scanf"%d%d%d%d",&nv,&ne,&c1,&C2); A      for(i=0; i<nv; i++) atscanf"%d",&weight[i]); - creategraph (); - Dijkstra (); -printf"%d%d\n", NUM[C2],W[C2]); - Printpath (C2); -     return 0; in } -  to voidcreategraph () + { -     inti,j; the     intV1,v2; *     intDN; $      for(i=0; i<nv; i++)Panax Notoginseng          for(j=0; j<nv; J + +) -g[i][j]=infi; the      for(i=0; i<ne; i++) +     { Ascanf"%d%d%d",&v1,&v2,&DN); theG[V1][V2]=G[V2][V1]=DN;//graph without Direction +     } - } $ voidDijkstra () $ { -     inti,j,k; -     intmin; the      for(i=0; i<nv; i++) -     {Wuyiknow[i]=0; theDistance[i]=g[c1][i];//update the distance from the starting point to all vertices -     } Wudistance[c1]=0;//The distance from the beginning to the beginning is 0 -w[c1]=WEIGHT[C1]; Aboutnum[c1]=1; $      for(i=1; i<nv; i++) -     { -k=-1; -Min =infi; A          for(j=0; j<nv; J + +) +         { the             if(!know[j] && distance[j]<min) -             { $k=J; theMin =Distance[j]; the             } the         } the         if(k==-1) Break; -know[k]=1;//find the shortest distance, mark the point in          for(j=0; j<nv; J + +) the         { the             if(!know[j] && min+g[k][j]<distance[j])//Update Distance About             { theDISTANCE[J] = min+G[k][j]; theNUM[J] =Num[k]; theW[J] = W[k] +Weight[j]; +pre[j]=K; -             } the             Else if(!know[j] && min+g[k][j]==Distance[j])Bayi             { thenum[j]=num[j]+Num[k]; the                 if(w[k]+weight[j]>W[j]) -                 { -w[j]=w[k]+Weight[j]; thepre[j]=K; the                 } the             } the         } -     } the } the  the voidPrintpath (intV//Recursive print path94 { the     if(v==C1) { theprintf"%d", v); the         return;98     } About Printpath (Pre[v]); -printf"%d", v);101}

l2-001. Emergency rescue (Dijkstra algorithm print path)

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.