PAT-A 真题- 1079. Total Sales of Supply Chain

发布于 / PAT-甲级 / 0 条评论

原题干:

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(例如这一节点售价为1元,比率为50%,则它的孩子节点售价为1*(1+50%)=1.5元)

接着给出N行数据,格式为Ki  ID1  ID2  ....  IDki

如果Ki为非0的数,则后面给出它的下一级节点号(节点号为0~~~N-1),如果Ki为0,则说明它是零售商,后面只给一个数字,表示它的产品库存量

最后输出所有叶子节点(零售商)所有产品价格之和

样例是这样的:

根节点的价格是1.8,利润率为1.00%,零售商节点(即叶子节点为4、7、8、9),4号节点的价格为1.8*1.01*1.01*7,7号节点总价格为1.8*1.01*1.01*9,8号节点总价格为1.8*1.01*1.01*1.01*4,9号节点总价格为1.8*1.01*1.01*1.01*3。累加求和即为42.36,保留一位小数为42.4

这道题用树+带深度的DFS比较好写。代码如下:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#define MAXN 100010
/*静态树数组*/
typedef struct{
    int sum;
    vector<int> child;
} node;
node tree[MAXN] = {0};
/*节点总数、根节点权、价格比率*/
int node_sum;
double root_price, rate;
/*价格总数(答案)*/
double ans = 0;
/*深度优先搜索*/
void DFS(int node_id, int depth){
    if(tree[node_id].child.size())  //当前不是叶子节点,递归访问孩子
        for(int i = 0; i < tree[node_id].child.size(); i++){
            DFS(tree[node_id].child[i], depth + 1);//深度+1
        }
        //当前是叶子节点,累加计算
    else ans += (root_price * pow(rate, depth))*tree[node_id].sum;
}

int main(){
    cin >> node_sum >> root_price >> rate;
    rate = 1 + 1.0*rate/100;
    for(int i = 0 ; i < node_sum; i++){
        int cnt;
        cin >> cnt;
        if(cnt)
            while(cnt--){
                int tmp_child;
                cin >> tmp_child;
                tree[i].child.push_back(tmp_child);
            }
        else cin >> tree[i].sum;
    }
    DFS(0, 0);
    printf("%.1f", ans);
    return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题- 1079. Total Sales of Supply Chain
目前还没有评论,快来抢沙发吧~