其他分享
首页 > 其他分享> > 1090 Highest Price in Supply Chain——PAT甲级真题

1090 Highest Price in Supply Chain——PAT甲级真题

作者:互联网

1090 Highest Price in 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. 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 highest price we can expect from some 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 they are numbered from 0 to N-1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si is the index of the supplier for the i-th member. Sroot for the root supplier is defined to be -1. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output:
1.85 2

题目大意:题目大致意思与1079 Total Sales of Supply Chain 相同,无非就是所求结果相同,这道题让你输出可以卖出的最高价格和能卖出最高价格的销售商的人数。

大致思路:利用DFS从根节点往下搜索,记录每个叶子结点的编号方便后续统计价格相同的个数。

代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;
struct node {
    vector<int> child;
    double sell;
}root[N];
double p, r;
int n;
vector<double> cost;
vector<int> leaf;
double ans = 0.0;

void newNode (int x) {
    root[x].sell = 0.0;
    root[x].child.clear();
}

void DFS(int x) {
    if (root[x].child.size() == 0) {
        ans = max(ans, root[x].sell);
        leaf.push_back(x);
        return;
    }
    for (int i = 0; i < root[x].child.size(); i++) {
        int tmp = root[x].child[i];
        root[tmp].sell = (1 + 0.01 * r) * root[x].sell;
        DFS(tmp);
    }
}

int main() {
    scanf("%d%lf%lf", &n, &p, &r);
    int root_index;
    for (int i = 0; i < n; i++) newNode(i);
    for (int i = 0; i < n; i++) {
        int s;
        scanf("%d",&s);
        if (s == -1) {
            root_index = i;
            continue;
        }
        root[s].child.push_back(i);
    }
    root[root_index].sell = p;
    DFS(root_index);
    int cnt = 0;
    for (auto l : leaf) {
        if (root[l].sell == ans) cnt++;
    }
    printf("%.2lf %d\n", ans, cnt);
    return 0;
}

标签:sell,PAT,Chain,Supply,int,price,child,supplier,root
来源: https://www.cnblogs.com/gxsoar/p/14395388.html