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 (≤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

思路

这题理解了题目是什么意思之后也是很简单的,用静态放方法建立一棵树,然后对每个叶子结点进行计算即可。
题目的内容可能比较多,说也说不清,我直接放张图叭:
在这里插入图片描述
下面我解释一下题目输入的数据:
第一行是结点个数N,商品单价P,以及每转手一次价格提升的百分比r,然后题目规定:结点0一定是供应商(即:0一定是根结点);
下面几行呢就是对应的父子关系,格式是:Ki ID[1] ID[2] … ID[Ki​​ ],其中,Ki的意思是当前结点i总共有几个孩子的意思,后面的ID就是孩子结点的编号。另外,如果Ki是0的话,就说明这是个叶子结点(也就是题目所说的零售商),然后后面跟的数是这个零售商的所销售的商品数量(我放在了data域里面)。

最后题目要求你输出所有零售商出售所有商品的总收益。关于怎么算呢,我图里也写出来了,其实就是个小学的问题……每转手一次,就意味着价格要提升(1+r%),因此,我们只需要计算转手几次,就能知道到最后零售商的单价是多少了(也就是P(1+r%)deep,这里的deep是叶子结点所在的深度,设根结点0的深度为0,因为供应商的价格就是P,不需要涨价)。

所以,我们只需要建完树之后,用DFS搜一遍就行了,如果当前是叶子结点,好,那么进行计算操作,然后返回。如果不是叶子结点,就遍历当前结点的孩子,往它孩子的方向继续dfs(这里不要忘了deep+1)。

代码

#include<cstdio>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 100010;
struct node{
    int data;
    vector<int> child;
    node(){
        data = 0;
        child.clear();
    }
}Node[maxn];
void dfs(int u, int deep, double P, double r, double &sum){
    if(Node[u].child.size()==0){//如果当前结点没有孩子,说明是叶子结点,即零售商
        sum += Node[u].data*P*pow(1+r/100, deep);
        return;
    }
    for(int i=0;i<Node[u].child.size();i++){
        int v = Node[u].child[i];
        dfs(v, deep+1, P, r, sum);
    }
}
int main(){
    int N;
    double P, r;
    scanf("%d%lf%lf", &N, &P, &r);
    for(int i=0;i<N;i++){
        int K;
        scanf("%d", &K);
        if(K==0){
            int tmp;
            scanf("%d", &tmp);
            Node[i].data = tmp;
        }
        else{
            for(int j=0;j<K;j++){
                int tmp;
                scanf("%d", &tmp);
                Node[i].child.push_back(tmp);
            }
        }
    }
    double sum = 0;
    dfs(0, 0, P, r, sum);//从结点0的第0层开始dfs
    printf("%.1f", sum);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值