1079 Total Sales of Supply Chain ——PAT甲级真题
1079 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.
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一次向下遍历。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node {
int id; //编号
vector<int> child; //孩子结点
int total; //卖出的总人数
double cell; //售价
}root[N];
int n;
double p, r, ans;
bool vis[N];
void newNode(int x) {
root[x].id = x;
root[x].child.clear();
root[x].total = 0;
}
void DFS(int x) {
//表示访问到根节点
if (root[x].total != 0) {
ans += root[x].total * root[x].cell;
return;
}
//访问x的子节点
for (int i = 0; i < root[x].child.size(); i++) {
int tmp = root[x].child[i]; //子节点编号
root[tmp].cell = (1 + r * 0.01) * root[x].cell;
DFS(tmp);
}
}
int main() {
cin >> n >> p >> r;
memset(vis, 0, sizeof(vis));
ans = 0.0;
for (int i = 0; i < n; i++) newNode(i);
for (int i = 0; i < n; i++) {
int k;
cin >> k;
if (k == 0) {
int num; cin >> num;
root[i].total = num;
continue;
}
for (int j = 0; j < k; j++) {
int x; cin >> x;
vis[x] = true; //非根节点标记一下
root[i].child.push_back(x);
}
}
int root_index; //根节点的编号
for (int i = 0; i < n; i++) {
if (!vis[i]) {
root_index = i;
break;
}
}
root[root_index].cell = p;
DFS(root_index);
printf("%.1lf\n", ans);
return 0;
}
1079 Total Sales of Supply Chain ——PAT甲级真题的更多相关文章
- 1090 Highest Price in Supply Chain——PAT甲级真题
1090 Highest Price in Supply Chain A supply chain is a network of retailers(零售商), distributors(经销商), ...
- PAT 甲级 1079 Total Sales of Supply Chain (25 分)(简单,不建树,bfs即可)
1079 Total Sales of Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributor ...
- PAT 1079 Total Sales of Supply Chain[比较]
1079 Total Sales of Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors(经 ...
- 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...
- PAT 甲级 1079 Total Sales of Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805388447170560 A supply chain is a ne ...
- PAT 1079. Total Sales of Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...
- 1079. Total Sales of Supply Chain (25)
时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...
- 1079. Total Sales of Supply Chain (25) -记录层的BFS改进
题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...
随机推荐
- JSP标签使用的代码记录——《%= %》(神奇的CSDN为啥标题不让打英文的尖括号)
关于JSP的一些标签,在用到的时候有些生疏,就去找了找资源重新温习了一下. 附上两个JSP<%= %>标签的博客,同时也记录当前项目里用到的方法. jsp页面中<%@ %>.& ...
- 输入DStream之基础数据源以及基于HDFS的实时wordcount程序
输入DStream之基础数据源以及基于HDFS的实时wordcount程序 一.Java方式 二.Scala方式 基于HDFS文件的实时计算,其实就是,监控一个HDFS目录,只要其中有新文件出现,就实 ...
- MySQL数据库的逻辑架构和存储引擎
和其他数据库相比,MySQL数据库的架构与众不同,它的架构可以在多种不同的场景中应用并发挥良好的作用:主要体现在存储引擎上的架构上,插件式的存储引擎架构将查询处理和其他的系统任务以及数据存储提取相分离 ...
- linux中在某个目录下多个文件中搜索关键字
有四种方法: find 文件目录 -name '*.*' -exec grep 'xxx' {} + -n 或是 find 文件目录 -name '*.*' | xargs grep 'xxx' -n ...
- priority_queue()大根堆和小根堆(二叉堆)
#include<iostream> #include <queue> using namespace std; int main() { //对于基础类型 默认是大顶堆 pr ...
- AtCoder AIsing Programming Contest 2020 D - Anything Goes to Zero (二进制,模拟)
题意:给你一个长度为\(n\)的\(01\)串,从高位到低位遍历,对该位取反,用得到的十进制数\(mod\)所有位上\(1\)的个数,不断循环直到为\(0\),输出每次遍历时循环的次数. 题解:根据题 ...
- Java基础(第二期)
数据类型扩展以及面试题讲解 整数拓展:进制 int i=10; int i2=010; //八进制0 int i3=0x10; //十六进制0x 0~9 A~F 16 相关进制转换自行学习,用的不多 ...
- Redis-sentinel 哨兵(HA)
Sentinel 介绍 Redis-Sentinel 是 Redis 官方推荐的高可用性(HA)解决方案,当用 Redis 做 Master-slave 的高可用方案时,假如Master 宕机了,Re ...
- IPC$入侵
一 唠叨一下: 网上关于ipc$入侵的文章可谓多如牛毛,而且也不乏优秀之作,攻击步骤甚至可以说已经成为经典的模式,因此也没人愿意再把这已经成为定式的东西拿出来摆弄. 二 什么是ipc$ IPC$(In ...
- μC/OS-III---I笔记13---中断管理
中断管理先看一下最常用的临界段进入的函数:进入临界段 OS_CRITICAL_ENTER() 退出临界段OS_CRITICAL_EXIT()他们两个的宏是这样的. 在使能中断延迟提交时: #if OS ...