题目描述
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a productfrom supplier to customer.
Starting from one root supplier, everyone on the chain buys products from one'ssupplier in a price P and sell or distribute them in a price that is r% higherthan P. Only the retailers will face thecustomers.
It is assumed that each member in the supply chain has exactly one supplierexcept the root supplier, and there is no supply cycle.
Now given a supply chain, you are supposed to tell the total sales from all theretailers.
输入描述:
Each input file contains one test case. For each case, the first line contains threepositive numbers: N (<=105), the total number of the members in the supply chain (andhence 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 ofprice increment for each distributor or retailer. Then N lines follow, each describes adistributor 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 receiveproducts from supplier i, and is then followed by the ID's of thesedistributors or retailers. Kj being 0 means that the j-th member is aretailer, then instead the total amount of the product will be given after Kj. All the numbers in a line are separated by a space.
输出描述:
For each test case, print in one line the total sales we canexpect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will notexceed 1010.
输入例子:
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
输出例子:
42.4
这一题的意思简单来说就是给你一棵树,每一个叶子节点代表一个经销商,每一个经销商都有销售商品的数量,根节点有一个价格,这棵树每下降一层价格就加上%r,问你所有经销商的商品价格总和为多少
首先可以根据输入的每个经销商的销售商品的数量来鉴别某个节点是否是叶子节点,如果是叶子节点,将总价格加上销售数量和价格的乘积,如果不是叶子节点,将价格加上%r,递归其所有儿子节点
需要注意的是printf在cstdio和stdio.h头文件里面都有这个函数,直接引用就好了,不过貌似不需要这两个头文件也可以使用这个函数!
#include <iostream>
#include <memory.h>
#include <cstdio>
#include <vector>
using namespace std;
vector<vector<int> > theEdge;
int theThingNum[100003];
double theAllPrize;
int N;
double P,r;
void _P(int t,int level,double prize)
{
if(theThingNum[t]!=-1)
{
theAllPrize+=prize*theThingNum[t];
}
else
{
for(int i=0;i<theEdge[t].size();i++)
{
_P(theEdge[t][i],level+1,prize*r);
}
}
}
int main()
{
int a,b;
cin>>N>>P>>r;
r=1.0+r/100;
theAllPrize=0;
memset(theThingNum,-1,sizeof(theThingNum));
theEdge = vector<vector<int> >(N);
for(int i=0;i<N;i++)
{
cin>>a;
if(a==0)
{
cin>>b;
theThingNum[i]=b;
}
else
{
for(int j=0;j<a;j++)
{
cin>>b;
theEdge[i].push_back(b);
}
}
}
_P(0,1,P);
printf("%.1lf",theAllPrize);
return 0;
}