PATA 1079 Total Sales of Supply Chain

insert image description here
insert image description here

Inscription

Ideas
This question is much easier to do if you understand the question. It probably means that the sales unit price of each node is %r higher than the unit price of the parent node, and the final retail store sales are calculated. First, a tree is established. This tree is not a binary tree, so a vector is needed to store the subscripts of the child nodes. Then find the height of each leaf node (used to calculate the unit price).
Specific steps
1. Establish the data structure of the node, read in the data, and establish a binary tree while reading in the data.
2. Perform root traversal, the root node's The depth is 0, and the depth of each layer is increased by one
. 3. Calculate the respective sales according to the depth of the leaf nodes and add
the error-prone points
. 1. Do not simply think that the depth of the child node is equal to the current when reading the data It is wrong to rush to update the record data by adding one to the node depth, because the depth of the current node may not be known yet.
2. Use double instead of float. The


code is as follows

#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
using namespace std;

const int Maxn=100010;

//输入
int n;
double P,r;

struct Node{
    
    
    int height=0;//存储深度(根结点为0)
    bool isretailer=false;//是否是零售店,若是为true
    double num=0.0;//货物数量(retailer为true才有效)
    vector<int> child;//存储孩子的下标
}node[Maxn];

void pre(int root,int height){
    
    //先根遍历找出每个结点的深度
    node[root].height=height;
    for(int i=0;i<node[root].child.size();i++)
        pre(node[root].child[i],height+1);
}

int main()
{
    
    
    double sum=0;//记录总的营销额

    scanf("%d %lf %lf",&n,&P,&r);
    r=r/100;
    for(int i=0;i<n;i++){
    
    
        int k;
        scanf("%d",&k);
        if(k==0){
    
    //这个结点是零售商
            node[i].isretailer=true;
            scanf("%lf",&node[i].num);
        }
        else{
    
    
            node[i].isretailer=false;
            int index_child;
            for(int j=0;j<k;j++){
    
    
                scanf("%d",&index_child);
                node[i].child.push_back(index_child);
            }
        }
    }

    pre(0,0);

    for(int i=0;i<n;i++){
    
    
        if(node[i].isretailer){
    
    
            sum+=P*node[i].num*pow(1+r,node[i].height);
        }
    }

    printf("%.1f\n",sum);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325392834&siteId=291194637