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.
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
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<vector> 5 #include<queue> 6 using namespace std; 7 typedef struct NODE{ 8 vector<int> child; 9 int depth; 10 int product; 11 }node; 12 node tree[100001]; 13 int N; 14 double P, r; //需要把/100 15 double bfs(int root){ 16 queue<int> Q; 17 double ans = 0; 18 if(tree[root].child.size() != 0){ 19 Q.push(root); 20 tree[root].depth = 0; 21 }else{ 22 ans = tree[root].product * 1.0 * P; 23 } 24 while(Q.empty() == false){ 25 int temp = Q.front(); 26 Q.pop(); 27 if(tree[temp].child.size() == 0){ 28 ans += (double)P * pow(1.0 + r, tree[temp].depth * 1.0) * 1.0 * tree[temp].product; 29 } 30 int len = tree[temp].child.size(); 31 for(int i = 0; i < len; i++){ 32 tree[tree[temp].child[i]].depth = tree[temp].depth + 1; 33 Q.push(tree[temp].child[i]); 34 } 35 } 36 return ans; 37 } 38 int main(){ 39 int cnt; 40 scanf("%d %lf %lf", &N, &P, &r); 41 r = r / 100.0; 42 for(int i = 0; i < N; i++){ 43 scanf("%d", &cnt); 44 if(cnt != 0){ 45 for(int j = 0; j < cnt; j++){ 46 int tempc; 47 scanf("%d", &tempc); 48 tree[i].child.push_back(tempc); 49 } 50 }else{ 51 scanf("%d", &tree[i].product); 52 } 53 } 54 double ans = bfs(0); 55 printf("%.1f", ans); 56 cin >> N; 57 return 0; 58 }
总结:
1、货物从供应商开始层层加价,然后计算最终总货物的价格。其实就是求每个叶子节点的深度,然后计算价格即可。
2、注意当只有一个根节点时的情况单独处理。
3、pow(a, b)函数:计算a的b次幂,要求a与b都是小数。所以当计算小数次幂时使用pow,计算整数时自己写函数。