PTA L2-001 Emergency Rescue (25 points)

This question is recorded because it is the most comprehensive algorithm question I have written to investigate the knowledge of graph theory. The meaning of the question is not difficult to understand. It has examined the shortest path in graph theory-Dijstkra algorithm, and expanded to the number of shortest paths and the largest Weight, shortest path, etc. I think it is more appropriate to review the knowledge point of the shortest path in graph theory.

L2-001 Emergency Rescue (25 points)

Title description

As the head of a city’s emergency rescue team, you have a special map of the country. On the map, there are multiple scattered cities and some express roads connecting the cities. The number of rescue teams in each city and the length of each expressway connecting the two cities are marked on the map. When other cities have emergency calls to you, your task is to lead your rescue team as soon as possible to the place where the incident occurred, and at the same time, gather as many rescue teams as possible 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, assume that the number of the city is 0 ~ (N−1); M is fast The number of roads; S is the city number of the departure place; D is the city number of the destination.

The second line gives N positive integers, where the i-th number is the number of rescue teams in the i-th city, and the numbers are separated by spaces. In the subsequent M lines, each line gives information about an express road, namely: city 1, city 2, and the length of the express road, separated by spaces, and the numbers are all integers and do not exceed 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 assembled. The second line outputs the number of the city passed in the path from S to D. The numbers are separated by spaces, and there should be no extra spaces at the end of the output.

Input sample

4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2

Sample output

2 60
0 1 3

#include <bits/stdc++.h>
#include <vector>
using namespace std;

struct Graph{
    
    
	int edge;
	int weight;
	Graph(int e, int w){
    
    
		edge = e;
		weight = w;
	}
};

int main(){
    
    
	int n, m, sx, sy;
	cin>>n>>m>>sx>>sy;
	int cs[n];		//记录救援队数目 
	int judge[n];	//记录该城市是否达到过
	int dotlist[n];	//记录权值 
	int pre[n];		//记录行走路径 
	int path[n];	//记录最短路径条数
	memset(path, 0, sizeof(path)); 
	memset(pre, 0, sizeof(pre));
	memset(dotlist, 0, sizeof(dotlist));
	memset(judge, 0, sizeof(judge));
	int res[n];
	fill(res, res+n, 501); 
	for(int i = 0; i < n; i++){
    
    
		cin>>cs[i];
	}
	vector<Graph> s[n];
	for(int i = 0; i < m; i++){
    
    
		int a, b, c;
		cin>>a>>b>>c;
		s[a].push_back(Graph(b, c));
		s[b].push_back(Graph(a, c));
	}
	res[sx] = 0;
	dotlist[sx] = cs[sx];
	path[sx] = 1;
	int flag = 0;			//记录目前城市的下标 
	for(int i = 0; i < n; i++){
    
    
		int min = 502;
		for(int j = 0; j < n; j++){
    
    
			if(!judge[j] && res[j] < min){
    
    
				min = res[j];
				flag = j;
			}
		}
		int num = res[flag];
		judge[flag] = 1;
		for(int j = 0; j < s[flag].size(); j++){
    
    
			int e = s[flag][j].edge;
			int w = s[flag][j].weight;
			if(!judge[e] && num + w < res[e]){
    
    
				res[e] = num + w;
				dotlist[e] = dotlist[flag] + cs[e];
				pre[e] = flag;
				path[e] = path[flag];
			}else if(num + w == res[e]){
    
    
				path[e] += path[flag];
				if(dotlist[flag] + cs[e] > dotlist[e]){
    
    
					dotlist[e] = dotlist[flag] + cs[e];
					pre[e] = flag;
				}
			}
		}
	}
	memset(res, 0, sizeof(res));
	int r = sy, k = 0;
	while(r != sx){
    
    
		res[k++] = pre[r];
		r = pre[r];
	}
	cout<<path[sy]<<' '<<dotlist[sy]<<endl;
	for(int i = k-1; i >= 0; i--){
    
    
		cout<<res[i]<<' ';
	}
	cout<<sy;
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_44723496/article/details/110220370