Pat【甲级】 1003 Emergency(附中文题目)

1003 Emergency

1003 Emergency (25分)
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, C​1 and C​2
​​ - 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 C​1 and C​2, 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

中文题目

作为城市的紧急救援团队负责人,你将获得一张你所在国家的特殊地图。

该地图显示了一些通过道路连接的分散城市,道路是双向的。

地图上标出了每个城市的救援队数量以及每对城市之间的每条道路的长度。

当其他城市发出紧急求援信息时,你的工作是尽快带领你的士兵前往该地点,同时,在途中尽可能多地调动救援帮手。

输入格式
第一行包含四个整数 N,表示城市数量(城市编号从 0 到 N−1),M 表示道路数量,C1 表示你当前所在的城市编号,C2 表示发出紧急求援信息的城市编号。

第二行包含 N 个整数,其中第 i 个整数表示城市 i 的救援队数量。

接下来 M 行,每行包含三个整数 c1,c2,Li,表示城市 c1 和城市 c2 之间存在一条道路相连,道路长度为 Li。

数据保证 C1 和 C2 之间至少存在一条路径相连。

输出格式
共一行,两个整数,第一个整数表示 C1 和 C2 之间最短路的数量,第二个整数表示走最短路的情况下,能聚集到的救援队最大数量。

数据范围
2≤N≤500,
1≤M≤600,
1≤Li≤200,
每个城市包含的救援人员数量不超过 200。

输入样例:
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
输出样例:
2 4

分析

首先我们要确定我们要用什么算法,从题意来看是要我们在求出最短路的基础上求出它的最短路方案数以及最大权重,所以我们要用dijkstra算法,在更新其他点的操作中+权重(到这一点最大救援队数量)的和最短方案数的更新,看下图👇

在这里插入图片描述

代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
const int N = 700;
int num[N];  //每个城市的救援队数量
int g[N][N];
int dist[N];
bool st[N];  //状态,是否已经更新过了
int res[N];  //存放权重
int sum[N];  //存放最短路径数目
int n, m, c1, c2;
void dijkstra() {
	memset(dist, 0x3f, sizeof(dist));
	dist[c1] = 0;
	res[c1] = num[c1], sum[c1] = 1;
	for (int i = 0; i < n; i++) {
		int t = -1;
		for (int j = 0; j < n; j++) 
			if (!st[j] && (t == -1 || dist[j] < dist[t]))t = j;

			st[t] = true;
		for (int j = 0; j < n; j++) {
			if (dist[j] > dist[t] + g[t][j]) {
				dist[j] = dist[t] + g[t][j];
				sum[j] = sum[t];
				res[j] = res[t] + num[j];

			}
			else if (dist[j] == dist[t] + g[t][j]) {
				sum[j] += sum[t];
				
				res[j] = max(res[j], res[t] + num[j]);
			}
				
		}
	}
}
int main() {
	cin >> n >> m >> c1 >> c2;
	for (int i = 0; i < n; i++) {
		cin >> num[i];
	}
	memset(g, 0x3f, sizeof(g));
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		g[a][b] = min(g[a][b], c);
		g[b][a] = min(g[b][a], c);;
	}
	dijkstra();
	cout << sum[c2] << " " << res[c2] << endl;
	return 0;
}

运行结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值