[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 <iostream>
#include <vector>
#include <queue>
#include <tgmath.h>
struct node {
    int id;
    std::vector<int> children;
};
int ans;
int deep;
void levelTraversal(std::vector<node *> &nodes) {
    if (nodes.size() <= 0) {
        return;
    }
    std::queue<node *> nodes_q;
    nodes_q.push(nodes[0]);
    while (!nodes_q.empty()) {
        deep++;
        std::queue<node *> 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<node *> 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會通過不了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章