PAT甲级1003:Emergency DFS解法(C++)

题目

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.

样例

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

题目分析

网上大多是Dijkstra,但我之前看郭炜老师的算法课,有一道非常相似的题目用的是DFS,所以第一时间就非常想用DFS写出来,结果总共用了快5个小时才完成,真的是太菜了呜呜o(╥﹏╥)o。

题目是最短路径问题,同时要兼顾救援人数最大。这里要特别注意题目给出的条件,可能会有多条最短路径,在这些最短路径中再选出救援人数最大的。因此DFS到达终点进行判断时,应该先判断路径长短,再对人数进行操作。

还有一个比较隐晦的点,题目没有说城市之间的路是单向的,因此在保存路的数据时应该保存正反两组。就是这个条件让我挠破头皮,卡了好久才发现问题所在,可能还是题型积累的不够吧。

代码

#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;
}

猜你喜欢

转载自blog.csdn.net/weixin_43390123/article/details/120048592