[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会通过不了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值