PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]
题目
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.
Now given a supply chain, you are supposed to tell the lowest price a customer can expect from some retailers.
Input Specification:
Each input file contains one test case. For each case, The first line contains three positive numbers: N(<=10^5), the total number of the members in the supply chain (and hence their ID’s are numbered from 0 to N-1, and the root supplier’s ID is 0); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:
Ki ID[1] ID[2] … ID[Ki]
where in the i-th line, Ki is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID’s of these distributors or retailers. Kj being 0 means that the j-th member is a retailer. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 1010.
Sample Input:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
000
Sample Output:
1.8362 2
题目分析
已知所有非叶子节点的所有子节点,求最短路径(根节点到叶子节点)的层数和该层叶子节点数
解题思路
思路 01
- dfs遍历树,参数h记录当前节点所在层数,统计所有层叶子结点数
- 找出出现叶子节点的最小层,并计算该层价格
思路 02
- dfs遍历树,参数p记录当前节点所在层的价格,min_h记录出现叶子节点的最小层,min_num记录max_p层叶子节点数
思路 03
- bfs遍历树,int h[n]数组记录当前节点所在层,统计所有层叶子结点数
- 找出出现叶子节点的最小层,计算该层价格
思路 04
- bfs遍历树,double ps[n]数组记录当前节点所在层的价格,min_h记录出现叶子节点的最小层,min_num记录max_p层叶子节点数
Code
Code 01(dfs)
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 100000;
vector<int> nds[maxn];
double p,r;
int cns[maxn],leaf[maxn],max_h;
void dfs(int index, int h){
max_h=max(max_h,h);
if(cns[index]==0){
leaf[h]++; //对应层级叶子节点数+1;
return;
}
for(int i=0;i<cns[index];i++){
dfs(nds[index][i],h+1);
}
}
int main(int argc, char * argv[]) {
int n,cid;
scanf("%d %lf %lf",&n,&p,&r);
for(int i=0; i<n; i++) {
scanf("%d",&cns[i]);
for(int j=0;j<cns[i];j++){
scanf("%d",&cid);
nds[i].push_back(cid);
}
}
dfs(0,0);
int min_h=0;
while(min_h<=max_h&&leaf[min_h]==0)min_h++;
printf("%.4f %d",p*pow(1+r*0.01,min_h),leaf[min_h]);
return 0;
}
Code 02(dfs 优化版)
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 100000;
vector<int> nds[maxn];
double p,r;
int cns[maxn],min_num,min_h=99999999;
void dfs(int index, int h){
if(min_h<h){
return;
}
if(cns[index]==0){
if(min_h==h){
min_num++;
}else if(min_h>h){
min_h=h;
min_num=1;
}
}
for(int i=0;i<cns[index];i++){
dfs(nds[index][i],h+1);
}
}
int main(int argc, char * argv[]) {
int n,cid;
scanf("%d %lf %lf",&n,&p,&r);
for(int i=0; i<n; i++) {
scanf("%d",&cns[i]);
for(int j=0;j<cns[i];j++){
scanf("%d",&cid);
nds[i].push_back(cid);
}
}
dfs(0,0);
printf("%.4f %d",p*pow(1+r*0.01,min_h),min_num);
return 0;
}
Code 03(bfs)
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=100000;
vector<int> nds[maxn];
double p,r;
int cns[maxn],cnt,max_h,h[maxn],leaf[maxn];
bool flag;
void bfs() {
queue<int> q;
q.push(0);
while(!q.empty()) {
int now = q.front();
q.pop();
max_h=max(max_h,h[now]);
if(cns[now]==0) {
leaf[h[now]]++; //对应层级叶子结点数+1;
} else {
for(int i=0; i<cns[now]; i++) {
h[nds[now][i]] = h[now]+1;
q.push(nds[now][i]);
}
}
}
}
int main(int argc,char * argv[]) {
int n,k,cid;
scanf("%d %lf %lf",&n,&p,&r);
for(int i=0; i<n; i++) {
scanf("%d",&cns[i]);
for(int j=0; j<cns[i]; j++) {
scanf("%d",&cid);
nds[i].push_back(cid);
}
}
h[0]=0;
bfs();
int min_h=0;
while(min_h<=max_h&&leaf[min_h]==0)min_h++;
printf("%.4f %d",p*pow(1+r*0.01,min_h),leaf[min_h]);
return 0;
}
Code 04(bfs 优化版)
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=100000;
vector<int> nds[maxn];
double p,r;
int cns[maxn],cnt,min_h=9999999,min_num,h[maxn];
bool flag;
void bfs() {
queue<int> q;
q.push(0);
while(!q.empty()) {
int now = q.front();
q.pop();
if(cns[now]==0) {
if(min_h>h[now]){
min_h=h[now];
min_num=1;
}else if(min_h==h[now]){
min_num++;
}
} else {
for(int i=0; i<cns[now]; i++) {
h[nds[now][i]] = h[now]+1;
q.push(nds[now][i]);
}
}
}
}
int main(int argc,char * argv[]) {
int n,k,cid;
scanf("%d %lf %lf",&n,&p,&r);
for(int i=0; i<n; i++) {
scanf("%d",&cns[i]);
for(int j=0; j<cns[i]; j++) {
scanf("%d",&cid);
nds[i].push_back(cid);
}
}
h[0]=0;
bfs();
printf("%.4f %d",p*pow(1+r*0.01,min_h),min_num);
return 0;
}
PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]的更多相关文章
- PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)
统计树的最小层数以及位于该层数上的叶子节点个数即可. 代码里建树我用了邻接链表的存储方式——链式前向星,不了解的可以参考,非常好用: http://www.cnblogs.com/chenxiwenr ...
- PAT甲级——1106 Lowest Price in Supply Chain(BFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90444872 1106 Lowest Price in Supp ...
- [建树(非二叉树)] 1106. Lowest Price in Supply Chain (25)
1106. Lowest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors( ...
- PAT 甲级 1106 Lowest Price in Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464 A supply chain is a ne ...
- 1106. Lowest Price in Supply Chain (25)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)–everyone inv ...
- PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...
- PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
简单dfs #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- 【PAT甲级】1106 Lowest Price in Supply Chain (25分)
题意:输入一个正整数N(<=1e5),两个小数P和R,分别表示树的结点个数和商品原价以及每下探一层会涨幅的百分比.输出叶子结点深度最小的商品价格和深度最小的叶子结点个数. trick: 测试点1 ...
随机推荐
- SQL——左连接(Left join)右连接(Right join)内连接(Inner join)
概念(定义) 首先还是介绍一下这三个的定义 Left join:即左连接,是以左表为基础,根据ON后给出的两表的条件将两表连接起来.结果会将左表所有的查询信息列出,而右表只列出ON后条件与左表满足 ...
- Centos 8下普通用户增加root权限
问题: 解决: 重启Centos,使用root登陆:
- ubuntu 中加速pip指令下载插件的速度
在使用pip下载时很多时候下载速度特别慢,时不时就会发生timeout. 这是因为安装源与本机之间网络不畅导致,其实可以自己指定pip的下载来源,就像指定ubuntu更新源那样. 接下来谈谈步骤: 1 ...
- Java中默认方法
默认方法是JDK8新特性,指的是接口也可以提供具体方法了,而不像以前,只能提供抽象方法,Mortal 这个接口,增加了一个默认方法 r,这个方法有实现体,并且被声明为了default,如以下代码: 这 ...
- UVA - 679 Dropping Balls(二叉树的编号)
题意:二叉树按层次遍历从1开始标号,所有叶子结点深度相同,每个结点开关初始状态皆为关闭,小球从根结点开始下落(小球落在结点开关上会使结点开关状态改变),若结点开关关闭,则小球往左走,否则往右走,给定二 ...
- 10nm Ice Lake处理器值得等待!
处理器.显卡等产品往往习惯先在 Linux 平台测试,所以 Linux 的内核源码往往成为曝光新品的宝藏之地. 经查,在 Linux v5.2 内核最新源码的 x86 分支中,出现了多款 Ice La ...
- 六十一、SAP中的逻辑运算与进制转换
一.代码如下 二.16进制计算过程如下 三.计算结果为16进制的11,也就是10进制的17
- Essay写作没逻辑不要慌,掌握这几点即可
今天文章的内容,真的是很多很多留学生的最大的问题,没有之一:逻辑.是的,你没有看错,也不用惊讶.大家的essay写作得分不高,很多时候不是因为语言问题.排除很多细节表达的不足,更让教授头疼的居然是:内 ...
- 使用docker快速体验kali linux
环境 运行在 64位 机器 企业版的 win10 系统 下载镜像 首先搜索docker download 去官网下载docker:https://www.docker.com/products/doc ...
- Java中定义常量(Constant) 的几种方法
为了方便大家交流Spark大数据,浪尖建了微信群,目前人数过多,只能通过浪尖或者在群里的朋友拉入群.纯技术交流,偶有吹水,但是打广告,不提醒,直接踢出.有兴趣加浪尖微信. 常量使用目的 1,为什么要将 ...