# L2-001 emergency rescue (25 points)

`As the head of a city's emergency rescue team, you have a special national map. There are many scattered cities and some fast roads connecting them on the map. The number of rescue teams in each city and the length of each fast track connecting the two cities are marked on the map. When other cities have emergency call to you, your task is to lead your rescue team to catch up with the past as soon as possible, and at the same time, gather as many rescue teams as possible along the way.

Input format:
Input the first line to give four positive integers N, M, S, D, where N (2 ≤ N ≤ 500) is the number of cities, by the way, assume that the number of cities is 0 ~ (N − 1); M is the number of expressways; S is the number of cities of departure; D is the number of cities of destination.

The second line gives N positive integers, where the number i is the number of rescue teams in the city i, separated by spaces. In the following M lines, each line gives the information of an expressway, which is the length of city 1, city 2 and expressway, separated by spaces, and the numbers are integers and no more than 500. The input ensures that the rescue is feasible and the optimal solution is unique.

Output format:
The first line outputs the number of shortest paths and the maximum number of rescue teams that can be called. The second line outputs the city number that passes in the path from S to D. Numbers are separated by spaces, and no extra spaces are allowed at the end of the output.

Input example:
4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2

Output example:
2 60
0 1 3

/*The first time I used the priority queue for a long time, but I didn't work it out. Then I went to have a good look at dijstra algorithm, and then I read what others wrote together with myself, and finally I did it (my own writing is still too little, so I advise freshmen to study hard). Come on, everyone*/

Insert a code slice here#include<iostream>
#include<queue>
#include<string.h>
using namespace std;

const int inf = 1e6;
int city[505][505];     //Used to mark the distance from one city to another
int pre[505];           //The pioneer of city in the shortest path
bool sign[505];         //Mark whether a city is in the shortest path
int arm[505];           //The number of troops in the city
int sum[505];           //Total number of troops passing through the shortest path in the current city
int dis[505];           //The shortest distance from the city to the starting point
int art[505] = {0};     //Number of shortest paths
struct node{
int n;         //City
int dis;       //The shortest distance from the city to the starting point
node(int _n,int _dis)
{
n = _n;
dis = _dis;
}
bool operator < (const node &a)const   //Priority queue priority, high priority near the starting point
{
return a.dis < dis;
}
};
int n,m,be,en;

void dijstra(int s)
{
memset(dis,inf,sizeof(dis));
dis[s] = 0;                        //The distance from the start point to the start point is 0
sum[s] = arm[s];				   //Total troops from start to start
art[s] = 1;							//The shortest path from start to start is 1
priority_queue<node>q;
q.push(node(s,dis[s]));
while(!q.empty())
{
node u = q.top();
q.pop();
if(sign[u.n])
continue;
sign[u.n] = true;
for(int i=0;i<505;i++)
{
if(city[u.n][i]!=inf)
{
if(sign[i])
continue;
if(dis[i] > dis[u.n] + city[u.n][i])		//Shorter distance
{
dis[i] = dis[u.n] + city[u.n][i];
sum[i] = arm[i] + sum[u.n];
pre[i] = u.n;
q.push(node(i,dis[i]));
art[i] = art[u.n];                     //Update the number of shortest paths
}
else if(dis[i] == dis[u.n]+city[u.n][i])	//Same distance, but more troops
{
if(sum[i] < sum[u.n] + arm[i])
{
sum[i] = arm[i] + sum[u.n];
pre[i] = u.n;
q.push(node(i,dis[i]));
}
art[i]+=art[u.n];                        //Update the number of shortest paths
}
}
}
}
}
void print(int be,int en)                                  //Traverse the city from the beginning to the end
{
if(be == en)
{
printf("%d",en);
return ;
}
else
{
print(be,pre[en]);
printf(" %d",en);
}
}

int main()
{
scanf("%d %d %d %d",&n,&m,&be,&en);
for(int i=0;i<n;i++)
{
int a;
scanf("%d",&a);
arm[i] = a;
}
memset(pre,-1,sizeof(pre));
memset(sum,0,sizeof(sum));
memset(sign,false,sizeof(sign));
for(int i=0;i<505;i++)
for(int j=0;j<505;j++)
city[i][j] = inf;
for(int i=0;i<m;i++)
{
int a,b,w;
scanf("%d %d %d",&a,&b,&w);
city[a][b] = w;
city[b][a] = w;
}
dijstra(be);
cout<<art[en]<<" "<<sum[en]<<endl;
print(be,en);
}
Published 1 original article · praised 0 · visited 4

Posted on Fri, 07 Feb 2020 03:29:31 -0500 by doa24uk