0
点赞
收藏
分享

微信扫一扫

C++PTA A1079(dfs可背诵)

豆丁趣 2022-01-27 阅读 50

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 (≤10 5), 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:

K i ID[1] ID[2] … ID[K i ] where in the i-th line, K i 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. K j being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after K j . 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 10 10

核心思路

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

这道题目理解真费劲,感觉自己的英文水平太差了。这个都看不懂,题目主要意思就是算出中间商赚了多少价格。就拿输入规格来看吧。10代表有10个供应商,1.80代表它的价格,1.00是百分之,比如这里的是1%,3 2 3 5是,3代表要输入的数量,2,3,5说明有三个零售商,0代表它是直接面向顾客了后面的数字代表它的销售量,

.在这里插入图片描述
输入的4代表卖了7,7卖了9,8卖了4,9卖了3.。因为4与7在同一层。所以他们的销量乘以1.81.011.01就是价格了,结点9与结点8又是同一层所以他们两相加再乘以1.81.011.01就是价格了。如何存储那么多结点呢,用vector

完整源码

#include<iostream>
#include<vector>
using namespace std;
struct node{
    vector<int>child;
    int amount;
};
node n[1000000];
int N;
double P,r;
double sum = 0;
void dfs(int cur,double cur_danjia){
    if(n[cur].child.size()) {
        cur_danjia*=(1+r/100);
        for(int each:n[cur].child){
            dfs(each,cur_danjia);
        }
    }else{
        sum += n[cur].amount*cur_danjia;
    }
}


int main()
{
    int i,j,k;
    cin >> N >> P >> r;
    for(i = 0;i<N;i++){
        cin>>j;
        if(j){
            while(j--){
              cin>> k;
            n[i].child.emplace_back(k);   
            }
           
        }else{
            cin >> n[i].amount;
        }
    }
    dfs(0,P);
    printf("%.1f",sum);
    return 0;
}


方法2:方便背诵版本

#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int maxn = 100010;
struct node {
    double data; // 数据域货物量
    vector<int>child; //指针域
}Node[maxn];
int n;
double p,r,ans = 0;//ans为结点货物的价格之和
void DFS(int index,int depth){
    if(Node[index].child.size() == 0){//到达叶节点
        ans += Node[index].data*pow(1+r,depth);
        return ;
    }
    for(int i = 0;i<Node[index].child.size();i++){
        DFS(Node[index].child[i],depth+1);//递归访问子节点
    }
}
int main()
{
    int k,child;
    scanf("%d%lf%lf",&n,&p,&r);
    r/=100;
    for(int i =0;i<n;i++){
        scanf("%d",&k);
        if(k==0){
            scanf("%lf",&Node[i].data);//叶节点货物量
        }else{
            for(int j = 0;j<k;j++){
                scanf("%d",&child);
                Node[i].child.push_back(child); // child为结点i的子节点
            }
        }
    }
    DFS(0,0);
    printf("%.1f\n",p*ans);
    return 0;
}


举报

相关推荐

0 条评论