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 after 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;
}

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

  1. 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  2. 1090. Highest Price in Supply Chain (25)-PAT甲级真题

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  3. PAT甲级 -- 1079 Total Sales of Supply Chain (25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  4. 1079. Total Sales of Supply Chain (25)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  5. 1079 Total Sales of Supply Chain(甲级)

    1079 Total Sales of Supply Chain (25分) A supply chain is a network of retailers(零售商), distributors(经 ...

  6. PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树、结点到根结点的距离、树形dp、记忆化搜索

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:下图是对样例的模拟图示,题目就是统计叶子结点卖出去的钱数.根据下图,我们第一步是建树,第二步是统计叶子结点到根结点的距离,然后才能知道 ...

  7. PAT 1079 Total Sales of Supply Chain[比较]

    1079 Total Sales of Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors(经 ...

  8. PAT 1079. Total Sales of Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  9. 1079 Total Sales of Supply Chain

    1. 这道题考察的是树的层次遍历,结点需要有层这个属性,对于我来说,难点在于什么时候给层赋值,看书后知道应该是在加入队列之前(不管是根节点还是之后所有节点). 2. 一开始算得216,原因是把r直接加 ...

最新文章

  1. 计算机网络_NAT与NAPT
  2. 在ashx中使用Session
  3. lazada发货_Lazada怎么发货?最全Lazada发货流程及注意事项!值得收藏!
  4. Dreamoon Likes Sequences CodeForces - 1330D(组合数学+位运算)
  5. HTTP/2 协议入门
  6. 怎么把一个网页作为背景套在另一个网页上_设计科技公司网站背景的六个小技巧...
  7. MongoDB,无模式文档型数据库简介
  8. 轻量级高并发物联网服务器接收程序源码(仅仅是接收硬件数据程序 ,没有web端
  9. 关于浮点数据类型和布尔数据类型以及最后的总结
  10. Pyechart绘制疫情发文可视化动态地图
  11. Flutter加载大图内存问题处理
  12. 风丘科技为您提供车载以太网解决方案
  13. GitHub 开源的超简单头像生成器,网友:好Q啊
  14. 自研还是采购BI系统?后悔知道得太晚!
  15. python读图命令与效率汇总
  16. AndroidStudio layout Inspector工具无法连接真机
  17. debian usb android,Debian下挂载usb设备
  18. 10个不得不买的高科技智能机器人
  19. java工具类怎么写_常用的Java工具类——十六种
  20. MDK5 Kil5中STM32工程的建立过程

热门文章

  1. 感谢星球日报、陀螺财经小伙伴的肯定,我们会继续努力哒
  2. 空间分析实验报告 实验二 度假村选址
  3. Image Processing and Analysis_8_Edge Detection:Learning to Detect Natural Image Boundaries Using Loc
  4. Java8使用stream操作两个list根据某字段匹配再对其中一个list进行赋值
  5. python的两种退出方式
  6. 洛谷 P4336 [SHOI2016] 黑暗前的幻想乡 题解
  7. 区块链钱包开发的前景
  8. 文本标注平台搭建之brat
  9. H.264/AVC 片——slice
  10. (附源码)ssm模具配件账单管理系统 毕业设计 081848