Pat甲级1003. Emergency (25)

这道题是一个最短路径问题

题目链接https://www.patest.cn/contests/pat-a-practise/1003

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.

题目样例

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
分析样例

5 6 0 2

有5个城市,6条路,求从编号0城市到编号2城市之间的最短路径以及能够到达的最多援救队。

1 2 1 5 3

表示编号0城市有一个援救队,编号1城市有两个援救队,编号2城市有1个援救队,编号3城市有5个援救队,编号4城市有3个援救队。

接下来的6行表示两个城市之间的路的长度。

题目分析清楚了,那就要开始做题了,写个dfs方法搜索一遍就出来了

具体看看dfs怎样写

让dfs停下来并符合题目要求的条件是

1.搜到的这个城市是要到达的城市

2.路径是最短的

3.人最多

我们需要设置两个全局变量用来保存最短的路径和最多的援救队数,

当搜索的城市到达目的地时,我们将此时的路径长度和最短路径长度作比较,如果比min小,就让min=它,否则就变,同理最多的救援队数也是如此。

再分析怎样搜索

用二维数组map[505][505]存储路径信息

用一维数组visit来表示是否访问过某个城市

从0-n个城市中找一个城市未被访问过并且和起点c1城市之间有路可走,如果找到了str1,将str1的访问值设为1,就再进行一次dfs,再从0-n中找一个城市未被访问过并且和str1之间有路可以走,直到找到的城市编号为目的地编号,然后就回溯,将这些城市编号的访问值设为0.如果没找到,则将此时的str的访问值设为0进行回溯。


#include<iostream>
using namespace std;
int n,m,c1,c2;
int a[505];
int map[505][505];
int visit[505];
int minm=1000000,mmax,sum;
void dfs(int str,int end,int s,int weight){
	if(str==end){
		if(s<minm){
			minm=s;
			sum=1;
			mmax=weight;
		}
		else if(s==minm){
			sum++;
			if(weight>mmax)
			mmax=weight;
		}
		return ;
	}
	if(s>minm)
	return ;
	for(int i=0;i<n;i++){
		if(visit[i]==0&&map[str][i]!=0){
			visit[i]=1;
			dfs(i,c2,s+map[str][i],weight+a[i]);
			visit[i]=0;
		}
	}
}
int main(){
	cin>>n>>m>>c1>>c2;
	for(int i=0;i<n;i++)
	cin>>a[i];
	int t1,t2,t3;
	for(int i=0;i<505;i++){
		visit[i]=0;
		for(int j=0;j<505;j++)
		map[i][j]=0;
	}
	for(int i=0;i<m;i++){
		cin>>t1>>t2>>t3;
		map[t1][t2]=map[t2][t1]=t3;
	}
	dfs(c1,c2,0,a[c1]);
	cout<<sum<<" "<<mmax<<endl;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值