PAT Class A 1003: Emergency DFS Solution (C++)

topic

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample

Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Output

2 4

Question analysis

Most of the ones on the Internet are Dijkstra, but I watched Teacher Guo Wei’s algorithm class before and there was a very similar question using DFS, so I wanted to write it in DFS right away, but it took almost 5 hours to complete. It’s really too good. O(╥﹏╥)o.

The topic is the shortest path problem, while taking into account the maximum number of rescuers. Pay special attention to the conditions given in the question here. There may be multiple shortest paths. Among these shortest paths, choose the one with the largest number of rescuers . Therefore, when DFS reaches the end point for judgment, it should first judge the length of the path and then operate on the number of people.

There is another obscure point. The question does not say that the roads between cities are one-way, so when saving the road data, you should save the positive and negative groups . It was this condition that made me scratch my head. It took me a long time to find out the problem. Maybe I didn't accumulate enough question types.

code

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
struct Road
{
    
    
	int endr, length;
};
int n, m, start, target;
int resnum[501]; //存放每个城市的救援人数
vector<vector<Road> > allroad(501); 
int visited[501];
int maxres;    //最大人数
int minlen;    //最短路径
int minlennum; //最短路径的数量
int totalres;  //当前人数
int totallen;  //当前路长

void dfs(int point)
{
    
    
	if (point == target) {
    
    
		if (totallen < minlen) {
    
     //有更短的路径
			minlen = totallen;   //更新最短路径
			maxres = totalres;   //更新该路径下的最大人数
			minlennum = 1;       //总数重新置为1
		}
		else if (totallen == minlen) {
    
     //有多条最短路径
			++minlennum;
			maxres = max(totalres, maxres);
		}
		return;
	}
	//对从point出发的每一条路进行DFS
	for (unsigned int i = 0; i < allroad[point].size(); i++)
	{
    
    
		if (totallen > minlen) {
    
     //一个小剪枝,当前路径已经大于记录的最短路径,直接返回
			return;
		}
		Road r = allroad[point][i];
		if (!visited[r.endr]) {
    
    
			visited[r.endr] = 1;
			totallen += r.length;
			totalres += resnum[r.endr];
			dfs(r.endr);
			visited[r.endr] = 0;
			totallen -= r.length;
			totalres -= resnum[r.endr];
		}
	}
}
int main()
{
    
    
	cin >> n >> m >> start >> target;
	for (int i = 0; i < n; i++)
	{
    
    
		int resque;
		cin >> resque;
		resnum[i] = resque;
	}
	for (int i = 0; i < m; i++)
	{
    
    
		int a;
		Road temp1, temp2;
		cin >> a >> temp1.endr >> temp1.length;
		temp2.endr = a;
		temp2.length = temp1.length;
		allroad[a].push_back(temp1); //正向道路
		allroad[temp1.endr].push_back(temp2); //反向道路
	}
	//初始化各种数据
	memset(visited, 0, sizeof(visited));
	visited[start] = 1;
	maxres = 0;
	minlen = 1 << 30;
	totallen = 0;
	totalres = resnum[start];
	dfs(start);
	cout << minlennum << " " << maxres;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43390123/article/details/120048592