PAT A1079. Total Sales of Supply Chain (25) [DFS, BFS,树的遍历]

【PAT】 PAT A1079.(两种解法)Total Sales of Supply Chain 题意 (原题为英文,这里直接介绍题目大意,节省大家读题的时间) 给出一棵供销树,树根唯一。在树根(根结点)处货物的价格为p,然后从根结点开始每往子结点走一层,货物的价格就在上一个结点价格的基础上增加 r%。现在给出每个叶子结点的货物量,求它们的价格之和。 输入样例 假设有一棵供销树如下: 输入样例和样例解释如下,请对照上图进行理解: 10 1.80 1.00 //结点总个数n、货物销售单价... 阅读详情

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 total sales from all the retailers.
Input Specification:
Each input file contains one test case. For each case, the first line contains three positive numbers: N
(<=105), 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 unit 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, then instead the total amount of the product will be given afer Kj. All the numbers in a line
are separated by a space.
Output Specification:
For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1
decimal place. It is guaranteed that the number will not exceed 1010.
Sample Input:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3
Sample Output:
42.4
题⽬⼤意:给⼀棵树,在树根出货物的价格为p,然后从根结点开始每往下⾛⼀层,该层的货物价格将
会在⽗亲结点的价格上增加r%,给出每个叶结点的货物量,求他们的价格之和
分析:树的遍历,可以采⽤dfs或者bfs两种⽅法。
采⽤dfs,建⽴结构体数组保存每⼀个结点的孩⼦结点的下标,如果没有孩⼦结点,就保存这个叶⼦结
点的data(销售的量)。深度优先遍历的递归出⼝,即当前下标的结点没有孩⼦结点的时候,就把ans
+= data(货物量) * pow(1 + r, depth)计算货物量*价格引起的涨幅百分⽐。如果有孩⼦结点,就dfs深
度优先遍历每⼀个孩⼦结点,并且在当前depth层数的基础上+1。最后输出ans * p(销售价格),即总
价格

#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
struct node {
	double data;
	vector<int> child;
};
vector<node> v;
double ans = 0.0, p, r;
void dfs(int index, int depth) {
	if(v[index].child.size() == 0) {
		ans += v[index].data * pow(1 + r, depth);
		return ;
	}
	for(int i = 0; i < v[index].child.size(); i++)
		dfs(v[index].child[i], depth + 1);
}
int main() {
	int n, k, c;
	scanf("%d %lf %lf", &n, &p, &r);
	r = r / 100;
	v.resize(n);
	for(int i = 0; i < n; i++) {
		scanf("%d", &k);
		if(k == 0) {
			scanf("%lf", &v[i].data);
		} else {
			for(int j = 0; j < k; j++) {
				scanf("%d", &c);
				v[i].child.push_back(c);
			}
		}
	}
dfs(0, 0);
printf("%.1f", ans * p);
return 0;
}
万字详解 PostgreSQL 的详细安装方式(Linux、Windows、macOS、Docker 全平台覆盖) 本文详细介绍了PostgreSQL在四大主流环境下的安装方法: Linux系统:针对Ubuntu/Debian和CentOS/RHEL系,分别提供APT/YUM仓库安装步骤,包括初始化配置和服务管理 Windows系统:通过图形化安装程序指导完成安装,包含组件选择、密码设置和pgAdmin配置 macOS系统:推荐使用Homebrew一键安装,支持Apple Silicon和Intel芯片 Docker容器:提供容器化部署方案(文中未展开) 文章强调安装前的版本选择、权限管理和存储规划,并针对各平台给出验证 阅读详情

相关推荐

Python离线断网情况下安装numpy、pandas和matplotlib等常用第三方包

联网情况下在命令终端CMD中输入“pip install numpy”即可自动安装,pandas和matplotlib同理一样方法进行自动安装。 工作的电脑不能上外网,所以不能通过直接输入pip命令来安装。 环境:电脑上已经安装了Python(没有安装的请参考其他教程)。 Python离线断网情况下安装numpy、pandas和matplotlib等常用第三方包的步骤如下: ...

dayou7738的博客 4942

PAT-A1106 Lowest Price in Supply Chain 题目内容及题解

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 th...

BourbonWhiskey 的博客 542

Silvaco TCAD仿真例库——肖特基二极管正向特性(例子)

此示例演示了肖特基二极管正向特性的仿真。 展示了: 使用 Atlas 语法形成二极管结构 设置阳极的肖特基势垒高度 正向偏置阳极 代码 第一部分:描述设备 mesh(网格) electrodes locations(电极位置) doping distribution(掺杂分布) 这是位于结构左侧和右侧的,2维n型器件,具有重掺杂浮动 p 型保护环区域, (2D n-type device with heavily doped floating p-type guard ring regio.

weixin_41788560的博客 2万+

[PAT A1079]Total Sales of Supply Chain

[PAT A1079]Total Sales of Supply Chain 题目描述 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. S...

欢迎来到我的编程小屋 842

保姆级教程:用STM32H743+TJA1042T玩转FDCAN,CubeMX配置1M/5M双速率通信(附完整代码)

本文提供STM32H743与TJA1042T实现FDCAN双速率通信的保姆级教程,详细讲解CubeMX配置1M/5M双速率通信的全过程,包括硬件连接、时钟树配置、过滤器设置及中断管理,并附完整代码示例,助力开发者快速掌握FDCAN高速通信技术。

weixin_30698297的博客 473

泰山派RK3576开发板部署Qwen3-VL-4B多模态模型实战指南:从模型转换到板端推理全流程

本文提供了在泰山派RK3576开发板上部署阿里Qwen3-VL-4B多模态模型的完整实战指南。详细介绍了从环境准备、模型转换(包括视觉模型转换为.rknn格式和语言模型转换为.rkllm格式)、交叉编译到板端推理的全流程,重点讲解了如何利用RK3576的NPU进行硬件加速,实现高效的边缘AI应用。

weixin_30356433的博客 257

PAT-A-1106 Lowest Price in Supply Chain (25分)

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 an

xsjzn的博客 218

布尔可满足性问题(SAT)深度研究

布尔可满足性问题(Boolean Satisfiability Problem, SAT)是计算机科学和数理逻辑中的一个核心决策问题。其定义为:对于一个给定的布尔逻辑公式,判断是否存在一组对其变量的真值赋值(即,为每个变量指定真 (True) 或假 (False)),使得该公式的最终计算结果为真。如果存在这样的赋值,则称该布尔公式是可满足的 (satisfiable);否则,称其为不可满足的 (unsatisfiable)。

步子哥的博客 496

PAT甲级 供应商问题 1079 1090 1106

供应商问题 (dfs) 1079 Total Sales of Supply Chain (25分) 题目描述 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to cu...

October の 补题记录 1390

包成功安装tiny-cuda-nn,记录安装过程中的问题解决,附带pytorch3d安装【踩坑指南】

该包可以显著提高NeRF训练速度,是和NeRFstudio等框架中,必须使用的。本文提供tiny-cuda-nn可以安装的完整包的下载链接,可以直接上传到项目中安装。附带笔者遇到的问题的解决方案。

zik的博客 7029

C# WPS操作PPT,全屏,缩率图,备注,跳转播放

本文介绍了一个使用C#开发的PPT操作工具,主要功能包括检测当前打开的PPT文件、获取选中PPT路径、指定播放PPT、获取PPT页面序号和缩略图、提取备注内容等。文章展示了操作界面效果图,并重点解决了用户手动退出PPT后重新获取路径时报错的问题。核心代码实现了通过WPS API进行PPT文件的打开、全屏播放、页面跳转、获取当前页位置等功能,同时支持生成PPT缩略图预览和备注信息提取。当用户未通过工具按钮打开PPT时,程序会自动初始化PPT信息确保功能正常使用。

遇见--专栏 310

PTA-树的遍历(1079/1090/1094/1106/1004/1053)

1079 Total Sales of Supply Chain (25 分) 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 pro.

qq_45929428的博客 1091

【高级雷达感知与学习】 第十三章 可解释性与物理–感知融合(Physics-Informed ML)

本文提出了一种面向雷达感知的物理约束学习方法,将雷达物理先验(RCS、SNR、距离衰减)嵌入神经网络,解决纯数据驱动模型的样本效率低、泛化能力弱和决策不可信问题。通过三种融合范式(混合建模、物理正则化、PINNs)实现机理与数据协同建模,并设计了物理引导显著图解决雷达可解释性语义错位问题。实验表明,物理约束可提升40%样本效率,增强低信噪比场景鲁棒性。代码实现包含物理正则化损失、可解释性分析和消融实验,为雷达感知系统提供了可迁移的工程范本。

VectorShift的博客 186

1079. Total Sales of Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

1079. Total Sales of Supply Chain (25) A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Start

柳婼 の blog 2332

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

PAT甲级1079 Total Sales of Supply Chain (25 分)题解

\quad树的遍历,需要记录每一个顶点是哪一层,当遍历到叶子节点时进行计算。可以用bfs或者dfs,如果数据量很大的情况下建议用bfs,不然可能爆栈。不过这个题数据量不大,给出bfs和dfs两个版本的程序。 BFS版本 #include <iostream> #include <vector> #include <queue> #include <cma...

qq_40438165的博客 1594

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Total Sales of Supply Chain (DFS和BFS两种实现)

1079 Total Sales of Supply Chain原题链接解题思路源代码DFSBFS 原题链接 PAT 甲级 1079 解题思路 这道题其实就是要知道叶结点的深度,最后的总价值就是: 叶结点处的权值*pow(1+r, 叶结点的高度),其中r是每个分销商或零售商的价格增量百分比。 那么求叶结点的深度就是树的遍历了,可以进行DFS或者BFS.DFS每次都一直沿一条分枝向下走直到遇到叶结点...

qq_40486952的博客 352

PAT A1079 Total Sales of Supply Chain (25分) C++/cpp实现

PAT A1079 Total Sales of Supply Chain (25分) 题目 原题链接 题目大意 给定树根的价格,每向下一层价格增加r,给出每个叶子节点货物的数量,计算叶子节点的价格总和, 大体思路 本题就是一个树的遍历问题,采用DFS或者BFS均可。这里采用DFS,利用树的深度depth代表层数,计算叶子节点的价格 代码 #include<stdio.h> #incl...

qq_41706836的博客 352

PAT 甲级 1079  Total Sales of Supply Chain

1079 Total Sales of Supply Chain (25 point(s)) A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. ...

漫浸天空的雨色的备忘录~ 607

PAT 1079 Total Sales of Supply Chain (25 分)

1079 Total Sales of Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting...

peanWang的博客 215

PAT-A-1079 Total Sales of Supply Chain (25 分) BFS广度优先搜索 C++题解

1079 Total Sales of Supply Chain (25 分) 题目传送门:1079 Total Sales of Supply Chain (25 分) 一、题目大意 商品销售链上有供应商、经销商、零售商,供应商以原价P将商品卖给经销商或者零售商,而经销商将会价格提高r%再卖给下一级的经销商或者零售商。零售商从上一级买到商品后,也会将价格提高r%,然后直接卖给用户。题目保证每一级...

爱玲姐姐的博客 998

PAT-ADVANCED1079——Total Sales of Supply Chain

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805388447170560 题目描述: 题目翻译: 1079 供应链总销售额 供应链是零售商,经销商和供应商构成的网络 - 每个...

清风阁 398

PAT 1079 Total Sales of Supply Chain

1079Total Sales of Supply Chain(25分) A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Startin...

YIU 251

PAT A1079 Total Sales of Supply Chain (25 分) 树遍历

题目大意:计算由 retailers(零售商), distributors(经销商), and suppliers(供应商)组成的树最终的销售额。其中,root supplier的节点编号是0,初始价格是p,每次向下一层,价格会增加 r%,直到叶子节点的 retailers,并给出所有retailers的销售量。 树的遍历问题,由于价格和层数有关系,dfs 和 bfs 都可以。 dfs代码: #include <vector> #include <cstdio> using name

HackingGate的博客 164

【PAT甲级】1079 Total Sales of Supply Chain (25分)

解题过程的小记录,如有错误欢迎指出。 难度:四星(树的先根遍历(DFS)和层次遍历(BFS)的应用) 小导航~题目分析注意点我的解题过程思路bug代码dalao的代码借鉴点 题目分析 一个供应链只有一个供应商,经过经销商价格会有涨幅,最后到达零售商手里卖出 给出0~n-1个结点的孩子(0为根节点,如果没有孩子则说明是零售商,则后续给出的是零售商的销量),要求算出所有零售商的销售额总额 注意点 经过一层一层经销商最后到达零售商的价格要用层次遍历,为每一层的价格赋值 我的解题过程 思路 设置node结构体,有

SpicyChicken的自我修养 418

PAT.A1079 Total Sales of Supply Chain

返回目录 题意 给出一颗树根编号为0的树,树根处货物价格为p,从根节点往下一层,货物价格就要增加r%,再给出叶子结点的货物量,问所有叶子结点的价格之和。 样例(可复制) 10 1.80 1.00 3 2 3 5 1 9 1 4 1 7 0 7 2 6 1 1 8 0 9 0 4 0 3 //output 42.4 注意点 本题使用DFS和BFS都可以,下面的写法使用的是DFS,如果使用BFS...

大模型天花板 359

PAT (Advanced Level) Practice 1079 Total Sales of Supply Chain(25分)【树的遍历、BFS/DFS】

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...

一只小羊 232

PAT A1079. Total Sales of Supply Chain (25)

原文链接: PAT A1079. Total Sales of Supply Chain (25) ...

阿豪 100
上一篇: PAT A1066. Root of AVL Tree (25) [平衡⼆叉树(AVL树) ]
下一篇: 【笔记】已知先序、中序,构建二叉树
志明er
博客等级 码龄7年 0粉丝 26原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值