zoukankan      html  css  js  c++  java
  • PAT甲级——1090 Highest Price in Supply Chain (BFS或者DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/94325835

    1090 Highest Price in Supply Chain (25 分)
     

    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

    题目大意:在一个供应链中找出商品售价最高的零售商的个数并求出价格。从源供应商root开始,每多一个中间商,就会加价 r%,消费者要从零售商处购买商品。

    思路:方法多样,这题跟1106没什么区别,用图论的最短(最长)路径也行,把它当成一棵树(有向图没有闭环),用DFS或者BFS都可以。这里把它当成一棵树,

    一、BFS记录深度,然后求最深的那一层的叶节点的数量即可。

     1 #include <iostream>
     2 #include <vector>
     3 #include <cmath>
     4 #include <queue>
     5 using namespace std;
     6 
     7 struct node {
     8     int level = 0;
     9     vector<int> child;
    10 };
    11 
    12 vector <node> chain;
    13 vector <int> retailer;//记录每层的叶节点(零售商)数量
    14 
    15 int getDepth(int& root);
    16 
    17 int main()
    18 {
    19     int N, root = 0;
    20     double P, r;
    21     scanf("%d%lf%lf", &N, &P, &r);
    22     chain.resize(N);
    23     retailer.resize(N, 0);
    24     for (int i = 0; i < N; i++) {
    25         int index;
    26         scanf("%d", &index);
    27         if (index != -1) {
    28             chain[index].child.push_back(i);
    29         }
    30         else {
    31             root = i;
    32         }
    33     }
    34     int depth = getDepth(root);
    35     printf("%.2lf %d
    ", P * pow(1 + r / 100, depth), retailer[depth]);
    36     return 0;
    37 }
    38 
    39 int getDepth(int& root) {
    40     int index = root,
    41         depth = 0;
    42     queue<int> Q;
    43     Q.push(index);
    44     while (!Q.empty()) {
    45         index = Q.front();
    46         Q.pop();
    47         if (chain[index].child.size() == 0)
    48             retailer[chain[index].level]++;
    49         if (depth < chain[index].level)
    50             depth = chain[index].level;
    51         int childIndex;
    52         for (int i = 0; i < chain[index].child.size(); i++) {
    53             childIndex = chain[index].child[i];
    54             chain[childIndex].level = chain[index].level + 1;
    55             Q.push(childIndex);
    56         }
    57     }
    58     return depth;
    59 }

     二、DFS求深度,同时记录每层的叶节点数量

     1 #include <iostream>
     2 #include <vector>
     3 #include <cmath>
     4 using namespace std;
     5 
     6 int depth = 0;
     7 vector<vector<int> > chain;
     8 vector<int> retailer;
     9 
    10 void DFS(int index, int level);
    11 
    12 int main()
    13 {
    14     int N, root = 0;
    15     double P, r;
    16     scanf("%d%lf%lf", &N, &P, &r);
    17     chain.resize(N);
    18     retailer.resize(N, 0);
    19     for (int i = 0; i < N; i++) {
    20         int index;
    21         scanf("%d", &index);
    22         if (index != -1)
    23             chain[index].push_back(i);
    24         else
    25             root = i;
    26     }
    27     DFS(root, 0);
    28     printf("%.2lf %d
    ", P * pow(1 + r / 100.0, depth), retailer[depth]);
    29     return 0;
    30 }
    31 
    32 void DFS(int index, int level) {
    33     if (depth < level)
    34         depth = level;
    35     if (chain[index].size() == 0)
    36         retailer[level]++;
    37     for (int i = 0; i < chain[index].size(); i++) {
    38         DFS(chain[index][i], level + 1);
    39     }
    40 }
  • 相关阅读:
    linux安装jdk(二)
    Java实现进程调度算法(一) FCFS(先来先服务)
    用Java Swing实现Freecell(空当接龙)
    windows主机开启MySQL慢查询日志
    CentOS LNMP 服务器安装配置详解
    shopex 网店系统 v4.8.5 安装图文教
    mysqldumpslow和mysqlsla分析mysql慢查询日志
    window下nginx配置
    PHP开发编码规范
    IIS+PHP+MySQL+Zend Guard Loader(ZendOptimizer)+phpMyAdmin环境配置图解(转)
  • 原文地址:https://www.cnblogs.com/yinhao-ing/p/11110027.html
Copyright © 2011-2022 走看看