2021-09-27 PAT甲级(1)

本文探讨了紧急情况下如何利用Dijkstra算法快速组织救援队伍,通过计算最短路径并最大化沿途救援队伍数量。实例解析了A+B格式求和与多项式求和问题,并介绍了城市救援队调度的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1001 A+B Format (20 分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

1002 A+B for Polynomials (25 分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

算法思想

1003 Emergency (25 分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:


1001 A+B Format (20 分)

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991
#include <stdio.h>
main(){
	int a,b;
	scanf("%d %d",&a,&b);
	int c = a+b;
	if(c<0)
	{
		printf("-");
		c = -c;
	}
	if(c>=1000000){
		printf("%d,%03d,%03d",c/1000000,c%1000000/1000,c%1000);
	}else if(c>=1000){
		printf("%d,%03d",c/1000,c%1000);
	}else{
		printf("%d",c);
	}
} 

注意点:输出的格式要用逗号。

收获了在多位数中添加逗号的方法。

1002 A+B for Polynomials (25 分)

This time, you are supposed to find A+B where A and B are two polynomials.

现在,这里有两个多项式A和B,需要你求出A+B.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1​ aN1​​ N2​ aN2​​ ... NK​ aNK​​

where K is the number of nonzero terms in the polynomial, Ni​ and aNi​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK​<⋯<N2​<N1​≤1000.

每个输入只包含一个测试样例。每个样例占2行,每行中包含多项式的信息: K N1 aN1 N2 aN2 ... NK aNK K表示多项式中非零项的个数,Ni和aNi(i=1,2,3...K)分别是正值数和系数,他们的取值范围分别为1<=K<=10,0<=NK< N2 N1<=1000

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

对于每一个测试样例,你只需用输入时相同的格式,在一行中输出A+B的和。注意在每行中没有多余的空格输出。请将系数精确的小数点后一位。

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2
#include <stdio.h>
main(){
	float c[1001] = {0};
	int k;
	scanf("%d",&k);
	int i;
	for(i=0;i<k;i++){
		int n;
		float m;
		scanf("%d %f",&n,&m);
		c[n] = c[n] + m;
	} 
	
	scanf("%d",&k);
	for(i=0;i<k;i++){
		int n;
		float m;
		scanf("%d %f",&n,&m);
		c[n] = c[n] + m;
	}
	
	int j=0;
	for(i=1000;i>=0;i--){
		if(c[i] != 0)
			j++;								
	} 
	printf("%d",j);
	for(i=1000;i>=0;i--){
		if(c[i] != 0){
			printf(" %d %.1f",i,c[i]);
		}											
	} 
} 

 英语没读懂。

注意题目中的数的大小限制,是在提醒你用数组。

算法思想

  1. 数据结构:可以选取数组来表示,其中用数组的下标来表示指数,数组元来表示系数。
  2. 算法:我们把多项式的每一个项初始化0,即数组的每个元素都为零。输入时对应相加。输出时倒序输出。注意非零项不要输出。

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, 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 Specification:

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

题目大意:
有 N 个城市 M 条道路,道路有权值,城市有一定数目的救援小组,起点 C​1​​ 终点 C​2​​。

输入案例:

第一行输入城市数N,依次城市的救援小组数M,起点c1和终点c2。

输出案例:

最短路径条数,最短路径上救援小组数目之和的最大值。

设计思路:
Dijkstra 算法

当节点出现最短路径,更新节点最短路径,最短路径数目,及救援队数目之和
当节点最短路径未改变,累加节点最短路径数目,更新救援队数目之和
 

#include<stdio.h>

#define INT_MAX 0x3ffffff

int main(){
	int n,m,c1,c2;
	scanf("%d %d %d %d",&n,&m,&c1,&c2);
	int visit[500] = {0};
	
	int i,j;
	int dis[500];
	for (i = 0; i < 510; i++)
        dis[i] = INT_MAX;
     	
	int weight[500]={INT_MAX};
	for(i = 0;i<n;i++){
		scanf("%d",&weight[i]);
	}
	
	int e[500][500];
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			e[i][j] = INT_MAX;
		}
	} 
	int a,b,v;
	for(j=0;j<m;j++){
		scanf("%d %d %d",&a,&b,&v);
		e[a][b] = v;
		e[b][a] = v;
	}
	
	int w[500];
	int num[500];
	dis[c1] = 0;
	w[c1] = weight[c1];
	num[c1] = 1; 
	
	//开始主循环,每次求得c1到某顶点的最短路径 
	for(int i=0;i<n;i++){
		int minn = INT_MAX;
		int u = -1;
		for(int j=0;j<n;j++){  //寻找离c1顶点的最近距离 
			if(visit[j] == 0 && dis[j] < minn){
				minn = dis[j];
				u = j;  //j离c1顶点更近 
			}
		}
		
		visit[u] = 1;//将目前找到的最近的顶点置为 1 
		
		for(v=0;v<n;v++){   //修正目前最短路径及距离 
			if(visit[v] == 0 && e[u][v] != INT_MAX){
				if(dis[v] > dis[u] + e[u][v]){
					dis[v] = dis[u] + e[u][v];
					w[v] = weight[v] + w[u];
					num[v] = num[u];		
				}else if(dis[u] + e[u][v] == dis[v]){//若距离相等,则说明有多条路径
						num[v] = num[v] + num[u];
						if(w[v] < w[u] + weight[v])
							w[v] = w[u] + weight[v];	
				}
			}
		}
	}
	printf("%d %d",num[c2],w[c2]);
	return 0;
}

 可以先画图理解题意,再设置变量,本题用二维数组的方法。

weight[]存储点权即各城市救援小组数目
dis[]表示从出发点到i结点的最短路径长度
num[]表示从出发点到i结点最短路径的条数
w[]表示从出发点到i结点的救援队的数量之和
visit[]标记i结点是否访问

               for (j = 0; j < n; j++) {  //寻找离c1顶点的最近距离 
                        if (visit[j] == 0 && dis[j] < minn) {
                                u = j;
                                minn = dis[j]; //j离c1顶点更近 
                        }
                }

这段代码不理解。j=0时,dis[0]=0。dis[j] < minn后,然后后面j再++就进不去这个if了呀,因为0是最小的呀???(黑人问号了)

解决:

第一遍循环出来后,visit[0]=1,第二遍后不会再进if里了。所以j++,dis[1] = ,以此类推。

初始化数组

dis[] = {INT_MAX};

for (i = 0; i < 500; i++)
        dis[i] = INT_MAX;

的区别:如果我们初始化输入的数字未达到定义长度,后面则默认初始化为0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值