PAT (Advanced Level) Practice 1003 Emergency

该博客讨论了一种特殊的城市紧急救援问题,其中作为救援团队领导,你需要从当前城市出发,通过最少时间到达目标城市,并沿途集结最多的救援队伍。文章介绍了一个基于Dijkstra算法的变形来解决这个问题,通过计算不同最短路径的数量和最大可能聚集的救援队伍数量,以优化救援行动。代码实现中包含了关键路径的计数和资源累计,为实际应急响应提供了有效的策略。

PAT (Advanced Level) Practice 1003 Emergency


Topic Description:

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)N (≤500)N(500) - the number of cities (and the cities are numbered from 0 to NNN− 1), MMM - the number of roads, C1C_1C1 and C2C_2C2 - the cities that you are currently in and that you must save, respectively. The next line contains NNN integers, where the iii-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,c2c_1, c_2c1,c2 and LLL, 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 C1C_1C1 to C2C_2C2.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1C_1C1 and C2C_2C2 , 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 Test:

Sample Input1:

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 Output1:

2 4

Simple analysis:

  • 一个Dijkstra的变形,详细见代码注释

My codes:

#include <iostream>
#include <cstring>

using namespace std;

const int N = 510;

int n, m, c1, c2;
int g[N][N], rescue[N];
int dist[N], cnt[N], tot[N];
// cnt代表最短路的数量综合,tot代表最短路时的最大救援人数
bool st[N];

void dijkstra() {
	memset(dist, 0x3f, sizeof dist);
	dist[c1] = 0, cnt[c1] = 1, tot[c1] = rescue[c1];

	for (int i = 0; i < n - 1; i++) {
		int t = -1;
		for (int j = 0; j < n; j++)
			if (!st[j] && (t == -1 || dist[t] > dist[j]))
				t = j;

		for (int j = 0; j < n; j++) {
			if (dist[j] > dist[t] + g[t][j]) {
				dist[j] = dist[t] + g[t][j];
				cnt[j] = cnt[t];
				tot[j] = tot[t] + rescue[j];
			}
			else if (dist[j] == dist[t] + g[t][j]) {
				cnt[j] += cnt[t];
				tot[j] = max(tot[j], tot[t] + rescue[j]);
			}
		}
		// 在模板的基础上主要增加了两个信息,cnt和tot

		st[t] = true;
	}
}

int main() {
	cin >> n >> m >> c1 >> c2;
	for (int i = 0; i < n; i++) cin >> rescue[i];
	memset(g, 0x3f, sizeof g);
	for (int i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		g[a][b] = g[b][a] = c;
	}

	dijkstra();

	cout << cnt[c2] << ' ' << tot[c2] << endl;

	return 0;
}
### 关于PAT Basic Level Practice的测试点及题目解析 #### 题目难度分级 PAT(Programming Ability Test)是由浙江大学举办的计算机程序设计能力考试,分为不同级别。其中乙级即Basic Level主要面向初学者,考察基本编程技能[^1]。 #### 测试点特点 对于PAT Basic Level中的某些特定题目而言,其测试点设置较为严格。例如,在处理字符串匹配类问题时,需要注意算法逻辑中何时应当终止循环以防止不必要的重复计算;而在涉及数值运算的问题里,则可能因为边界条件而增加复杂度[^3]。 #### 编程语言的选择影响 值得注意的是,尽管大部分简单题目可以作为学习某种新语言的良好实践材料,但在实际操作过程中可能会遇到由于所选语言特性而导致难以通过全部测试点的情况。比如Java在面对部分效率敏感型试题时表现不佳,这可能是由于该语言本身的执行速度相对较慢以及内存管理方式等因素造成的。因此有时不得不转而采用其他更适合解决此类问题的语言版本来完成解答[^2]。 ```cpp #include<bits/stdc++.h> using namespace std; int a[100000]; int c=1; void getPrime(){ int flag=0; for(int i=2;i<105000;i++){ flag=1; for(int j=2;j<=sqrt(i);j++){ if(i%j==0){ flag=0; break; } } if(flag==1) a[c++]=i; } } int main(){ int m,n,i,t=1; scanf("%d %d",&m,&n); getPrime(); for(i=m;i<=n;i++){ if(t%10==1){ printf("%d",a[i]); t++; }else{ printf(" %d",a[i]); t++; } if((t-1)%10==0) printf("\n"); } return 0; } ``` 上述C++代码展示了如何实现一个简单的质数打印功能,并且针对输出格式进行了特殊处理以满足特定要求。这段代码很好地体现了编写高效解决方案的重要性,尤其是在应对像PAT这样的在线评测系统时[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值