PAT甲级1003. Emergency (25)

题目链接

https://www.patest.cn/contests/pat-a-practise/1003

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

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

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就可以很好的解决掉。

用到的数组

map[][]邻接矩阵保存边的权值

visited[]判断该节点是否访问过

city_value[]保存节点权值

代码

#include<iostream>
#include<cstring>
using namespace std;

const int N = 500;
int city_value[N];
int map[N][N];
bool visited[N];
int city_num,road_num,start,end_;
int Min_path = 100000;//记录最小path长度
int Min_count = 0;//记录最小path个数
int Max_num = 0;//记录最大人数

void dfs(int start,int length,int num)
{
	if(start == end_)
	{
		if(length > Min_path)//判断1 
			return ;
		if(length < Min_path)//判断2
		{
			Min_path = length;
			Min_count = 1;
			Max_num = num;
		}
		
		else//最小path相同 Min_count++
			{
				Min_count++;
				Max_num = Max_num > num?Max_num:num;
			}	
		//出错原因思考 判断1和判断2 颠倒顺序
		//else的匹配问题 假设先有 判断1 则else的条件肯定为 length = Min_path的条件
		//假设 先有 判断2 且判断2 执行了 此时到判断1时 与else匹配 else的条件为 length<=Min_path 刚才Min_count =1 ,现在Min_count++ 为2 
	}

	if(length > Min_path)//剪枝
		return ;
	for(int k = 0;k < city_num ; k++)//开始遍历所有与start城市相连的城市
	{
		if(map[start][k] == -1 || visited[k] == true)
			continue;
		visited[k] = true;
		dfs(k,length+map[start][k],num+city_value[k]);
		visited[k] = false;//注意
	}
}

int main()
{
	memset(visited,0,sizeof(visited));
	memset(city_value,0,sizeof(city_value));
	memset(map,-1,sizeof(map));
	//三个数组初始化
	cin>>city_num>>road_num>>start>>end_;
	for(int i = 0;i < city_num;i++)
		cin>>city_value[i];
	for(int j = 0;j < road_num;j++)
	{
		int a,b,len;
		cin>>a>>b>>len;
		map[a][b] = map[b][a] = len;
	}
	visited[start] = true;
	dfs(start,0,city_value[start]);
	visited[start] = false;
	cout<<Min_count<<" "<<Max_num<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值