l2-001. Emergency rescue time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen
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
Idea: The enhanced version of the Dijkstra algorithm
1#include <bits/stdc++.h>2 #defineINF 1e9+73 using namespacestd;4stack<int>Q;5 structnode{6 Long Longvalue=-1;7 intcost=inf;8 intPointvalue;9 intvis=0;Ten intCnt=0; One intFA; A }; -Node arr[505]; - intmat[505][505]; the intCheckintN) - { - for(intI=0; i<n;i++) - if(arr[i].vis==0)return 1; + return 0; - } + voidDijkstraintStartintEndintN) A { at intnow=start; -Arr[start].cnt=1; -arr[start].vis=1; -Arr[start].value=Arr[start].pointvalue; -arr[start].cost=0; -Arr[start].fa=start; in for(intI=0; i<n;i++) - { to intmaxx=inf; + for(intj=0; j<n;j++)//relaxion - { the if(NOW==J)Continue; * if(arr[now].cost!=inf&&arr[now].cost+mat[now][j]<arr[j].cost&&arr[j].vis==0) $ {Panax Notoginsengarr[j].cost=arr[now].cost+Mat[now][j]; -Arr[j].value=arr[now].value+Arr[j].pointvalue; theArr[j].fa=Now ; +Arr[j].cnt=arr[now].cnt; A } the Else if(arr[now].cost+mat[now][j]==arr[j].cost) + { -arr[j].cnt+=arr[now].cnt; $ if(arr[j].value<arr[now].value+arr[j].pointvalue) $ { -Arr[j].value=arr[now].value+Arr[j].pointvalue; -Arr[j].fa=Now ; the } - }Wuyi } the for(intj=0; j<n;j++) - { Wu if(NOW==J)Continue; - if(arr[j].vis==0&&arr[j].cost<Maxx) About { $maxx=Arr[j].cost; -now=J; - } - } Aarr[now].vis=1; + } the } - intMain () $ { the intN,m,start,end; theCin>>n>>m>>start>>end; the for(intI=0; i<n;i++) theCin>>Arr[i].pointvalue; - for(intI=0; i<n;i++) in { the for(intj=0; j<n;j++) the { Aboutmat[i][j]=inf; the } the } the for(intI=0; i<m;i++) + { - ints,e,k; theCin>>s>>e>>K;Bayimat[s][e]=mat[e][s]=K; the } the Dijkstra (start,end,n); -cout<<arr[end].cnt<<' '<<Arr[end].value; - while(arr[end].fa!=end) the { the Q.push (end); theEnd=Arr[end].fa; the } -cout<<Endl; the Q.push (start); the while(!q.empty ()) the {94cout<<q.top (); the Q.pop (); the if(!q.empty ()) thecout<<' ';98 } About return 0; -}
l2-001. Emergency assistance (Dijkstra)