zoukankan      html  css  js  c++  java
  • PAT甲级——A1090 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 (≤), the total number of the members in the supply chain (and hence they are numbered from 0 to N1); 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 −. 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 1.

    Sample Input:

    9 1.80 1.00
    1 5 4 4 -1 4 5 3 6
    

    Sample Output:

    1.85 2


     1 #include <iostream>
     2 #include <vector>
     3 #include <queue>
     4 #include <cmath>
     5 using namespace std;
     6 int N, root, maxL = 0, resN = 1, level[100100] = { 0 };
     7 double p, r;
     8 vector<int>G[100100];;
     9 void BFS(int root)
    10 {
    11     queue<int>q;
    12     q.push(root);
    13     while (!q.empty())
    14     {
    15         root = q.front();
    16         q.pop();
    17         for (auto a : G[root])
    18         {
    19             level[a] = level[root] + 1;
    20             if (maxL < level[a])
    21             {
    22                 maxL = level[a];
    23                 resN = 1;
    24             }
    25             else if (maxL == level[a])
    26                 resN++;
    27             if (G[a].size() > 0)
    28                 q.push(a);
    29         }
    30     }
    31 }
    32 void DFS(int root)
    33 {
    34     if (G[root].size() == 0)
    35         return;
    36     for (auto a : G[root])
    37     {
    38         level[a] = level[root] + 1;
    39         if (maxL < level[a])
    40         {
    41             maxL = level[a];
    42             resN = 1;
    43         }
    44         else if (maxL == level[a])
    45             resN++;
    46         DFS(a);
    47     }
    48 }
    49 int main()
    50 {
    51     cin >> N >> p >> r;
    52     int a;
    53     for (int i = 0; i < N; ++i)
    54     {
    55         cin >> a;
    56         if (a == -1)
    57             root = i;
    58         else
    59             G[a].push_back(i);        
    60     }
    61     //BFS(root);
    62     DFS(root);
    63     printf("%.2f %d
    ", p*pow(1.0 + r / 100.0, maxL), resN);
    64     return 0;
    65 }
  • 相关阅读:
    ES6, CommonJS, AMD, CMD,UMD模块化规范介绍及使用
    前端项目开发框架选型需考虑的4个方面
    初识webSocket及其使用
    动态组件 —— 2种方式实现动态组件的切换
    mac下anaconda安装新包
    新版docker设置国内镜像
    记一次解决Original error: UiAutomator quit before it successfully launched
    linux clion cmakelisits undefined reference 未定义引用
    苹果设备插入PC不能识别问题解决办法
    用Cucumber理解BDD行为驱动开发
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11345053.html
Copyright © 2011-2022 走看看