PAT 1003. Emergency (25)---深搜

作为城市应急救援队队长,你需要解决一张显示国家各城市及其连接道路的地图。每个城市都有标注的救援队伍数量,每条道路长度也已给出。当接到某城市的紧急呼叫时,你的任务是以最快的速度前往并沿途召集尽可能多的增援。输入包含多个测试案例,每个案例给出城市数量、道路数量、起始城市和目标城市,以及各城市救援队伍数量和道路详情。你需要找出从起点到终点的所有最短路径数量,并计算最大可能集结的救援队伍总数。
摘要由CSDN通过智能技术生成

1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

#include <iostream>
using namespace std;
const int INFINITY=1000;

int n,m,loc,dtm;
int* mark;
int* teams;
int** map;
int mindis;
int maxtem;
int num;

void dfs(int str,int end,int dis,int teamsum){
	if(dis>INFINITY)return;
	if(str==end){
		if(dis<mindis){
			num=1;
			mindis=dis;
				maxtem=teamsum;
		}else if(dis==mindis){
			num++;
			if(teamsum>maxtem)
				maxtem=teamsum;
		}
		return;
	}

	for(int i=0;i<n;i++){
		if(mark[i]==0&&map[str][i]<INFINITY){
			mark[i]=1;
			dfs(i,end,dis+map[str][i],teamsum+teams[i]);
			mark[i]=0;
		}
	}
}

int main(){
	cin>>n>>m>>loc>>dtm;
	teams=new int[n];
	mark=new int[n];
	map=new int*[n];
	for(int i=0;i<n;i++)
	{
		cin>>teams[i];
		mark[i]=0;
		map[i]=new int[n];
		for(int j=0;j<n;j++){
			map[i][j]=INFINITY;
		}
	}
	for(int j=0;j<m;j++){
		int a,b,x;
		cin>>a>>b>>x;
		map[a][b]=map[b][a]=x;
	}
	mindis=INFINITY;
	dfs(loc,dtm,0,teams[loc]);
	cout<<num<<" "<<maxtem;
	//system("pause");
	return 0;
}
最后一个测试点超时。未解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值