PAT甲级 -- 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 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?(≤10?5??), the total number of the members in the supply chain (and hence their ID's are numbered from 0 toN?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:
K?i???ID[1] ID[2] ... ID[K?i??]
where in the?i-th line,?K?i???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.?K?j???being 0 means that the?j-th member is a retailer, then instead the total amount of the product will be given after?K?j??. 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?10?10??.
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 3Sample Output:
42.4思路:
1. 和1106,1090 題目屬于同一個系列,輸入有變化,但是結構沒有變化
2. 輸入時當 k == 0時,后面跟銷售的產品數量,在結構體中增加 productNum來記錄,遍歷到達葉子結點進行計算。
#include <iostream> #include <vector> using namespace std; struct node{double price;int childNum;int productNum;vector<int> child; }; vector<node> supply; int N; double r; //N個chain上的人,r%的比率 double P; //P為最初的價格 double totoalPrice = 0; int cnt = 0; void dfs(int root, double p){supply[root].price = p;if(supply[root].childNum == 0){ //到達葉子結點totoalPrice += supply[root].price * supply[root].productNum;}for (int i = 0; i < supply[root].child.size(); i++){dfs(supply[root].child[i], p * (100+r)/100.0);} } int main(){scanf("%d %lf %lf", &N, &P, &r);supply.resize(N);for (int i = 0; i < N; i++){int k;scanf("%d", &k);if(k == 0) {supply[i].childNum = k;scanf("%d",&supply[i].productNum);}else{supply[i].childNum = k;supply[i].child.resize(k);}for(int j = 0; j < k; j++){scanf("%d", &supply[i].child[j]);}}dfs(0,P);printf("%.1f", totoalPrice);return 0; }?
總結
以上是生活随笔為你收集整理的PAT甲级 -- 1079 Total Sales of Supply Chain (25 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT甲级 -- 1103 Intege
- 下一篇: PAT甲级 -- 1053 Path o