1003 Emergency (25 分)-PAT甲级

本文介绍了一位紧急救援队队长如何利用地图信息,通过深度优先搜索找到两个城市之间的最短路径,并在此过程中最大化救援团队集结。通过DFS算法,理解如何在最短路径下计算最大救援队伍规模。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Sample Output:

2 4

 题目大意:给定一张包含多个城市的地图,地图包含信息有城市名称、城市之间的道路及其长度、每个城市的成员人数,要求求出给定两个城市的最短路径以及在满足最短路径下的最大救援人数

参考了弄夫 学长的思路,视频讲的很好,大家可以去看看。

思路:对地图进行DFS搜索,同时标记各个顶点到出发点的最短距离,当搜索到目的点的时候,更新目的地的最短距离以及最大救援人数。

实现原理为:DFS搜索属于盲目搜索,在没有visited数组标记访问状态时,DFS搜索会尝试每一种可能的路径(图中可能的路径都会执行一遍),因此在这种情况下,我们只要控制好递归返回条件,使DFS搜索遍历出每一种从出发点到目的地的可能路径,在这些路径中选择最短路径并统计,在最短路径的前提下,统计最大救援人数即可。

说明:除了使用DFS外,还可以使用BFS、Dijkstra算法来解决这道题,博主以后会更新解法!

 AC代码(DFS版本)

#include <iostream>
#include <vector> 
using namespace std;
int lens[500][500];//城市之间的距离 
int num[500];//城市人数 
vector<int>city[500];//存储城市map
int c1,c2,path_cnt,teams;//出发地和目的地 ,最短路径的个数,最大救援人数 
int dist[500];//记录从出发地到当前城市的最短距离 
int team[500]={0};//从出发点开始到各顶点最终携带的最大救援人数 
void dfs(int cur_city,int cur_dist,int cur_team)
{
	if(cur_dist>dist[cur_city])//当前距离大于最短距离,这种情况不需要 
		return;					//遍历下去了 
	if(cur_city==c2)//找到了目的地 
	{
		if(cur_dist<dist[cur_city])//当前最短距离小于最短距离 ,更新最短距离 
		{
			path_cnt=1;
			dist[cur_city]=cur_dist;
			teams=cur_team;//更新最大救援人数 
		}
		else
		{
			path_cnt++;//最短距离一样就在原有数量的基础上增加1 
			if(cur_team>teams)//更新最大救援人数 
				teams=cur_team;
		}
	}
	else
	{
		if(cur_dist<dist[cur_city])//更新最短距离 
			dist[cur_city]=cur_dist;
		for(int i=0;i<city[cur_city].size();i++)//继续遍历相邻顶点,注意看实参的变化 
			dfs(city[cur_city][i],cur_dist+lens[cur_city][city[cur_city][i]],cur_team+num[city[cur_city][i]]);
	}
} 
int main()
{
	int n,m,temp1,temp2,len;
	cin>>n>>m>>c1>>c2;
	for(int i=0;i<n;i++)
		cin>>num[i];
	for(int i=0;i<m;i++)
	{
		cin>>temp1>>temp2>>len;
		city[temp1].push_back(temp2);//构建map 
		city[temp2].push_back(temp1);
		lens[temp1][temp2]=lens[temp2][temp1]=len;//利用二维数组保存城市之间的道路长度 
	}
	fill(dist,dist+500,100000000);//各顶点到出发点的距离默认为很大 
	dfs(c1,0,num[c1]);//实参初始化为c1的情况 
	cout<<path_cnt<<" "<<teams;
	return 0;
}

更多PAT甲级题目,请访问PAT甲级刷题之路--题目索引+知识点分析(正在进行),感谢支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值