当前位置:网站首页>PAT 甲级 A1079 Total Sales of Supply Chain
PAT 甲级 A1079 Total Sales of Supply Chain
2022-07-15 14:02:00 【柘木木】
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题意:
有供应商(根节点),经销商(非叶子叶子的子节点)和零售商(叶子结点),构成的一棵树,只有零售商有货物量(权重),给出结点的个数和货物价格以及每经过一个店面就要涨的百分比,求这棵树的所有零售商所有货物的总价格(零售商的所在的层可能不一样)。
思路:
和1090思路差不多,叶子结点多了个权重而已,先建树,然后遍历树,算出最大价值就好了。
代码:
//编号和结点的关系的树,用树的静态写法;
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=100010;
int child,n,k;
double p,r,ans=0; //ans应该是double 类型;
struct node{
double data;//货物量(权重),只有叶子结点有;
vector<int > child;
}Node[maxn];
void DFS(int index,int depth){
if(Node[index].child.size()==0){
ans+=Node[index].data*pow(1+r,depth);
return ;//递归边界;;
}
for(int i=0;i<Node[index].child.size();i++)
DFS(Node[index].child[i],depth+1);
}
int main( ){
scanf("%d %lf %lf",&n,&p,&r);
r/=100;
//建树,赋值:
for(int i=0;i<n;i++){
scanf("%d",&k);//输入i结点下子结点的数目;
if(k==0){//没有子节点就是叶子结点,只有叶子结点有货物量(带权);
scanf("%lf",&Node[i].data);
}else{
for(int j=0;j<k;j++){//子节点编号;
scanf("%d",&child);
Node[i].child.push_back(child);
}
}
}
//遍历树;
DFS(0,0);
printf("%.1f",p*ans);//价格*上浮幅度=上涨价格,价格*上浮幅度*货物量=总货物量上涨后的总价值,所以可以先将p提出来,算出各个结点的浮幅度*货物量,最后再乘以原价格也可以得到总价值。
return 0;
}边栏推荐
猜你喜欢

Flowable query the current user's to-do task method and report an error

zabbix 监控服务 (三) 配置管理图形和窗口

把一个数组封装成类

MGRE and OSPF

记一次 .NET 某电厂Web系统 内存泄漏分析

JS figure guess fruit and vegetable games code

JS image clipping and compression integration plug-in

Deploy eshopondapr on Tencent cloud eks

【第二十二题】地下城与勇士(北理工/北京理工大学/程序设计方法与实践/小学期 )

Intel IPU
随机推荐
Mysql8.0 learning records 18 - tablespaces
JVM垃圾收集之——怎样判定一个对象是不是垃圾
荷兰蒂尔堡大学、联邦大学 | Neural Data-to-Text Generation Based on Small Datasets: Comparing the Added Value of Two Semi-Supervised Learning Approaches on Top of a Large Language Model(基于小数据集的神经数据到文本生成)
如何使用Fiddler进行弱网测试
leetcode 605. Can place flowers planting problem (simple)
关于Anaconda的一些操作(安装软件和快速打开)
Interprocess communication -- signal principle and detailed explanation
Technology sharing | common interface protocol analysis
响应式用户登录表单
40+倍提升,详解 JuiceFS 元数据备份恢复性能优化之路
Maximum sub segment and + segment tree one
如何查看是否有清华源/删除清华源,保留默认源
从源码探究双亲委派机制
Upload pictures / files
2022-07-15日报:Meta宣布推出Make-A-Scene:可基于文字和草图控制AI图像生成
leetcode 605. Can Place Flowers 种花问题 (简单)
SaaS application: the best way to realize enterprise digital transformation
面试题:fail-safe 机制与 fail-fast 机制分别有什 么作用
技术分享 | 常见接口协议解析
Technology sharing | sending requests using curl