A1079
Description:
在将产品从供应商流通至消费者的过程中包含零售商、经销商和供应商三个角色。
从一个根供应商起,每人将价格定位进价的(100+r)%,只有零售商会面对消费者。保证供应链上的每个校色只有一个供应商(除了根供应商)。
给一个供应链,求所有零售商的总销售额。
同系列题目:
Input:
-
每个样例首行为三个正数:
- 供应链上的成员总数N<=1e5,其编号为[0,N-1],根供应商的ID为0;
- 根供应商指定的单位价格P
- 价格增长率r
-
接下来是N行,每行如此描述经销商或是零售商:
Ki ID[1] ID[2] … ID[Ki]
位于第i行时,Ki是从i号供应商接收产品的经销商或零售商总数,然后是这些经销商或者零售商的ID。Kj为0以为着第j位成员是一个零售商,在Kj后将给出产品总数。
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
Output:
每个测试样例,打印我们能从所有零售商得到的总销售额,保留一位小数,保证这个数字将不会超过1e10。
42.4
深搜:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1e5+5;
struct node{
int num; //携带产品数量,非零售商为0
vector<int>child; //孩子节点
}Node[maxn];
double price, r, ans = 0;
int n;
void dfs(int nin, double np){ //当前节点下标nin,当前价格Now Price = np
if(Node[nin].child.empty()){ //到达叶子节点
ans += (np*Node[nin].num);
return ;
}
vector<int>::iterator it = Node[nin].child.begin();
for(; it != Node[nin].child.end(); it++){ //遍历所有孩子节点
dfs(*it, np*(100+r)/100); //传递新价格
}
return ;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
scanf("%d%lf%lf", &n, &price, &r);
int totle, tid;
for(int i = 0; i < n; i++){
scanf("%d", &totle);
if(totle == 0){ //该节点为零售商
scanf("%d", &Node[i].num);
}
else{
for(int j = 0; j < totle; j++){
scanf("%d", &tid);
Node[i].child.push_back(tid);
}
}
}
dfs(0, price);
printf("%.1f\n", ans);
return 0;
}