PAT 1079 Total Sales of Supply Chain(树DFS遍历)

1079 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

题目大意:给出总的供应链的成员,根节点的单价,每一次转手后价格提高的百分比(需要题目没有给出百分号),第几行对应第一个成员,第一个数字表示有几个供应对象,后面的数组表示供应的对象,如果供应的对象数量为0的话,则表示该成员是零售商,后面的数字表示的是需要的商品数量

总结:这道题目其实不难,有两个点,①:价格计算搞错了,原来题目给的是提高的百分比

②遍历的时候循环写出了

代码:

#include <iostream>
#include <cstring>
using namespace std;

const int N=100010;
int h[N],e[N],ne[N],idx;
int sum[N];
bool st[N];
int n;
double p,r,total;

void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(double price,int t){
    st[t]=true;
    if(h[t]==-1)    total+=price*sum[t];
    for(int i=h[t];i!=-1;i=ne[i]){
        int j=e[i];
        if(!st[j])  dfs(price*(1+r),j);
    }
}

int main(){
    scanf("%d %lf %lf",&n,&p,&r);
    r/=100;
    memset(h,-1,sizeof h);
    
    int t,w;
    for(int i=0;i<n;i++){
        scanf("%d",&t);
        if(t==0)    scanf("%d",&sum[i]);//,如果供应对象为0的话,表示零售商需要的数量
        else{
            for(int j=0;j<t;j++){
                scanf("%d",&w);
                add(i,w);
            }
        }
    }
    dfs(p,0);    
    printf("%.1lf\n",total);
    return 0;
}

柳神的代码:

总的价格=商品的数量*对应的单价(此处单价=原始单价*上涨幅度)

#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
struct node {
    double data;
    vector<int> child;
};
vector<node> v;
double ans = 0.0, p, r;
 
void dfs(int index, int depth) {
    if(v[index].child.size() == 0) {
        ans += v[index].data * pow(1 + r, depth);
        return ;
    }
    for(int i = 0; i < v[index].child.size(); i++)
        dfs(v[index].child[i], depth + 1);
}
int main() {
    int n, k, c;
    scanf("%d %lf %lf", &n, &p, &r);
    r = r / 100;
    v.resize(n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &k);
        if(k == 0) {
            scanf("%lf", &v[i].data);
        } else {
            for(int j = 0; j < k; j++) {
                scanf("%d", &c);
                v[i].child.push_back(c);
            }
        }
    }
    dfs(0, 0);
    printf("%.1f", ans * p);
    return 0;
}

好好学习,天天向上!

我要考研!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值