第二天

A1003 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

单词

emergency

英 /ɪ'mɜːdʒ(ə)nsɪ/ 美 /ɪ'mɝdʒənsi/

n. 紧急情况;突发事件;非常时刻

adj. 紧急的;备用的

rescue

英 /'reskjuː/ 美 /'rɛskju/

n. 营救,解救,援救;营救行动

v. 营救,援救;(非正式)防止……丢失

scattered

英 /'skætəd/ 美 /'skætɚd/

adj. 分散的;散乱的

at the mean time

同时

call up

打电话给;召集;使想起;提出

as many hands

尽可能多的人手

guarantee

英 /gær(ə)n'tiː/ 美 /,ɡærən'ti/

n. 保证;担保;保证人;保证书;抵押品

vt. 保证;担保

题目分析

本题是正常的单源最短路问题,使用Dijkstra算法即可解决,只不过在刷新dist(到达每一点的最短路)的时候需要顺便需要刷新num(最短路的条数)与rescue(到达某一点的救援队数量)的数量,关于最短路的条数num,我一开始想的比较简单,只使用了一个整型变量来监测到达终点时的最短路条数,实际上关于最短路的条数和最短路的长度和救援队的数量一样需要不断刷新才能得到最后的结果。

具体代码

#include&ltstdio.h&gt
#include&ltstdlib.h&gt
#include&ltlimits.h&gt
#define MAXSIZE 500 int road[MAXSIZE][MAXSIZE];
int team[MAXSIZE];
int rescue[MAXSIZE];
int is_collect[MAXSIZE];
int is_visited[MAXSIZE];
int dist[MAXSIZE];
int num[MAXSIZE];
int N, M, C1, C2; void Dijkstra(); int main(void)
{
scanf("%d %d %d %d", &N, &M, &C1, &C2);
for (int i = 0; i rescue[i])
rescue[i] = rescue[m] + team[i];
}
is_visited[i] = 1;
}
}
}
}

参考博客

1003. Emergency (25)-PAT甲级真题(Dijkstra算法)

A1004 Counting Leaves (30 分)

题目内容

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of nodes in a tree, and M (<N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

Sample Input:

2 1

01 1 02

Sample Output:

0 1

单词

hierarchy

英 /'haɪərɑːkɪ/ 美 /'haɪərɑrki/

n. 层级;等级制度

sake

英 /seɪk/ 美 /sek/

n. 目的;利益;理由;日本米酒

n. (Sake)人名;(罗)萨克;(日)酒(姓)

processed

英 /p'rəsest/ 美 /p'rəsest/

adj. (食品)经过特殊加工的;精制的;(矿石等)处理过的

v. 加工;审核;处理(数据);列队行进;冲印(照片);把(头发)弄成直发(process 的过去式及过去分词)

supposed

英 /sə'pəʊzd/ 美 /sə'pozd/

v. 认为,推断;假定,设想;(婉转表达)我看,要不;必须做;(理论)要求以……为前提(suppose 的过去式和过去分词)

adj. 假定的,据说的;被要求的;被允许的;坚信的,期望的

seniority

英 /siːnɪ'ɒrɪtɪ/ 美 /,sinɪ'ɔrəti/

n. 年长;级别、职位高;资历;(前任者、老资格的)特权

题目分析

本题最大的问题是英文并不能读懂。。。。。题目的大意是给定一个树的结点和非叶子结点数量,以及每个非叶子结点的儿子数量和编号,要求按层次输出每一层的叶子结点数量。

首先题目并未说明这棵树到底是说明树,所以并不能把它当成一个二叉树,普通树的物理表示方法其实我很少接触和实践,所以用了何老师说的儿子-兄弟表示法。

然后就是计算每一层的叶子结点的数量,我用一个整型数组来存储每一层的数量,然后使用递归计算每层的数量。最后再一起输出。具体代码如下。

具体代码

#include&ltstdio.h&gt
#include&ltstdlib.h&gt struct node
{
int son;
int brother;
}; struct node tree[100];
int not_root[100];
int N, M;
int max = -1;
int leaf[100]; void count_leaf(int root, int level); int main(void)
{
scanf("%d %d", &N, &M);
for (int i = 0; i max)
max = level;
int last = root;
while (last != 0)
{
if (tree[last].son == 0)
leaf[level]++;
else
count_leaf(tree[last].son, level + 1);
last = tree[last].brother;
}
}
}

PTA A1003&A1004的更多相关文章

  1. 浙大PTA - - 堆中的路径

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...

  2. 浙大PTA - - File Transfer

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...

  3. ERROR<53761> - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...

    LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Direc ...

  4. PTA中提交Java程序的一些套路

    201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...

  5. PTA分享码-Java

    主要用于Java语法练习,非竞赛类题目.   1. Java入门          959dbf0b7729daa61d379ec95fb8ddb0   2. Java基本语法   23bd8870e ...

  6. C语言第一次实验报告————PTA实验1.2.3内容

    一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...

  7. PTA题---求两个有序序列中位数所体现的思想。

    ---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A​0​​,A​1​​, ...

  8. 第十四,十五周PTA作业

    1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...

  9. 第七周PTA作业

    第一题: #include<stdio.h> int main() { ; ; ){ sum=sum+i; i++; } printf("sum = %d\n",sum ...

随机推荐

  1. Django Mysql数据库-F查询和Q查询

    一.F查询和Q查询 F查询: 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F() 的 ...

  2. 第一章 .NET基础-C#基础

    一.1.1. 基础语法 一.1.1.1. 注释符 一.1.1.1.1. 注释符的作用 l 注释 l 解释 一.1.1.1.2. C#中的3中注释符 l 单行注释 // l 多上注释 /* 要注释的内容 ...

  3. springboot中oracle的依赖添加失败的解决

    由于Oracle授权问题,Maven3不提供oracle JDBC driver  步骤一:在pom中添加如下: <!--Oracle 驱动 --> <dependency> ...

  4. bilibili弹幕爬取与比对分析

    最近受人之托研究了下b站的数据爬取做个小工具,最后朋友说不需要了,本着开源共享的原则,将研究成果与大家分享一波,话不多说直接上干货 需求分析 给定up主uid和用户uid,爬取用户在该up主所有视频中 ...

  5. 记录一则DG遭遇ORA-00088的案例

    测试环境:RHEL 5.4 + Oracle 11.2.0.3 DG 现象:起初是在使用DG Broker进行switchover切换测试时,报错ORA-16775,提示有可能有数据丢失,不允许swi ...

  6. Leetcode之回溯法专题-78. 子集(Subsets)

    Leetcode之回溯法专题-78. 子集(Subsets) 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = ...

  7. 重温Android和Fragment生命周期

    重温下Android和Fragment生命周期,理解生命周期方法的作用,什么时候调用,可以做一些什么操作. 1.Android生命周期 1.1 生命周期图 1.2 生命周期函数说明 onCreate: ...

  8. codeforces 807 C. Success Rate(二分)

    题目链接:http://codeforces.com/contest/807/problem/C 题意:记 AC 率为当前 AC 提交的数量 x / 总提交量 y .已知最喜欢的 AC 率为 p/q ...

  9. PHP 实现字符串表达式计算

    什么是字符串表达式?即,将我们常见的表达式文本写到了字符串中,如:"$age >= 20",$age 的值是动态的整型变量. 什么是字符串表达式计算?即,我们需要一段程序来执 ...

  10. 了解css中px、em、rem的区别并使用Flexible实现vue移动端的适配

    本人java菜鸟一名,若有错误,还请见谅. 1.px和em和rem的定义和区别 px:px像素,是相对单位,相对于屏幕的分辨率而言,也就是说,当屏幕的分辨率不同那么px相同,实际看到的大小也会不同. ...