[PAT]1106 Lowest Price in Supply Chain (25分)(样例3,6,7未通过原因)

一、题目

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 lowest price a customer 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 their ID’s are numbered from 0 to N−1, and the root supplier’s ID is 0); P, the 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. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 1010
Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
0
0
0

Sample Output:

1.8362 2

二、题目翻译

一条供应链是一个由零售商,经销商供应商组成的网络,这三者将商品从供应商手中传递到客户手中。
从根供应商开始,供应链上的其他参与者都会从上级手中以价格P购买商品,并以比P高r%的价格卖给下级。只有零售商可以直接将商品卖给客户。假设供应链中的每个参与者只有一个上级(除了根供应商),并且供应链中不存在环。
给出一个供应链,请你给出客户可以买到商品的最低价格(和以最低价格卖商品的零售商的数量)。

三、代码

#include 
#include 
#include 
#include 
struct node {
    int id;
    std::vector children;
};
int ans;
int deep;
void levelTraversal(std::vector &nodes) {
    if (nodes.size() <= 0) {
        return;
    }
    std::queue nodes_q;
    nodes_q.push(nodes[0]);
    while (!nodes_q.empty()) {
        deep++;
        std::queue nodes_q_2;
        while (!nodes_q.empty()) {
            node *temp_node = nodes_q.front();
            nodes_q.pop();
            if (temp_node->children.size() <= 0) {
                ans++;
            } else {
                for (int i = 0; i < temp_node->children.size(); i++) {
                    nodes_q_2.push(nodes[temp_node->children[i]]);
                }
            }
        }
        if (ans >= 1) {
            return;
        }
        while (!nodes_q_2.empty()) {
            node *temp_node = nodes_q_2.front();
            nodes_q_2.pop();
            nodes_q.push(temp_node);
        }
    }
}

int main() {
    double price, rate;
    int n;
    std::cin >> n >> price >> rate;
    std::vector nodes(n);
    for (int i = 0; i < n; i++) {
        int num;
        std::cin >> num;
        node *temp_node_p = new node();
        temp_node_p->id = i;
        nodes[i] = temp_node_p;
        for (int j = 0; j < num; ++j) {
            int temp_num;
            std::cin >> temp_num;
            nodes[i]->children.push_back(temp_num);
        }
    }
    levelTraversal(nodes);
    price = price * std::pow(1.0 + (rate)/100, deep - 1);
    printf("%.4f %d", price, ans);
    return 0;
}

四、注意

  1. 题目要求输出的是;最低价格(the lowest price)和以最低价格卖商品的零售商的数量(and the number of retailsers that sell at the lowest price)。
  2. 要使用double类型,不能使用float类型,不然样例3,6,7会通过不了。

你可能感兴趣的:(PAT)