zoukankan      html  css  js  c++  java
  • PAT A1106 Lowest Price in Supply Chain (25 分)——树的bfs遍历

    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 N1, 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
    
     
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <set>
     4 #include <string.h>
     5 #include <vector>
     6 #include <math.h>
     7 #include <queue>
     8 using namespace std;
     9 const int maxn = 100011;
    10 int n;
    11 double p,r;
    12 vector<int> res[maxn];
    13 int num=0,lvl=0;
    14 void bfs(int root){
    15     queue<int> q;
    16     q.push(root);
    17     int flag=0;
    18     while(!q.empty()){
    19         queue<int> tmp;
    20         while(!q.empty()){
    21             int now = q.front();
    22             q.pop();
    23             if(res[now].empty()){
    24                 num++;
    25                 flag=1;
    26             }
    27             for(int i=0;i<res[now].size();i++){
    28                 tmp.push(res[now][i]);
    29             }
    30         }
    31         if(flag)break;
    32         while(!tmp.empty()){
    33             q.push(tmp.front());
    34             tmp.pop();
    35         }
    36         lvl++;
    37     }
    38 }
    39 int main(){
    40     scanf("%d %lf %lf",&n,&p,&r);
    41     int i;
    42     for(i=0;i<n;i++){
    43         int k;
    44         scanf("%d",&k);
    45         for(int j=0;j<k;j++){
    46             int x;
    47             scanf("%d",&x);
    48             res[i].push_back(x);
    49         }
    50     }
    51     bfs(0);
    52     p=p*pow((1+r/100),lvl);
    53     //printf("%d",lvl);
    54     printf("%.4f %d",p,num);
    55 }
    View Code

    注意点:bfs遍历要一层一层保存和入队,用正常的一个队列进行bfs会出现不该入队的元素入队,导致结果错误。例如原本第二层会到达终点,但队列里的第二层元素不是第一个元素就到达终点了,而是后面几个,这样第一个元素的子节点也已经入队,此时如果子节点也到了终点就会多算。

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    js给redio设置哪一个被选中
    问候struts2升级的新版本2.5
    图片上传与显示时候的路径问题
    Json,String,Map之间的转换
    springboot之web
    关于struts2中出现的漏洞,如何测试解决
    Table-valued functions and Scalar-valued functions in SQL Server
    Using system view: sys.sysprocesses to check SqlServer's block and deadlock
    利用sys.sysprocesses检查SqlServer的阻塞和死锁
    SQLSERVER加密解密函数(非对称密钥 证书加密 对称密钥)
  • 原文地址:https://www.cnblogs.com/tccbj/p/10451229.html
Copyright © 2011-2022 走看看