zoukankan      html  css  js  c++  java
  • pat1090. Highest Price in Supply Chain (25)

    1090. Highest Price in Supply Chain (25)

    时间限制
    200 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 (<=105), the total number of the members in the supply chain (and hence they are numbered from 0 to N-1); 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 -1. 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 1010.

    Sample Input:
    9 1.80 1.00
    1 5 4 4 -1 4 5 3 6
    
    Sample Output:
    1.85 2
    

    提交代码

    每次只让不为叶结点的节点入队。BFS。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<stack>
     4 #include<algorithm>
     5 #include<iostream>
     6 #include<stack>
     7 #include<set>
     8 #include<map>
     9 #include<vector>
    10 #include<queue>
    11 using namespace std;
    12 map<int,vector<int> > edge;
    13 int main()
    14 {
    15     //freopen("D:\INPUT.txt","r",stdin);
    16     int n,i,num,core;
    17     double price,r;
    18     scanf("%d %lf %lf",&n,&price,&r);
    19     for(i=0;i<n;i++){
    20         scanf("%d",&num);
    21         if(num==-1){
    22             core=i;
    23         }
    24         else{
    25             edge[num].push_back(i);
    26         }
    27     }
    28     int last=core,e=core,cur;
    29     queue<int> q;
    30     int maxcount=1,count=0;
    31     r/=100;
    32     if(edge[core].size()){//只让下面还有节点的点入队,不让叶结点入队
    33         q.push(core);
    34     }
    35 
    36     //cout<<price<<endl;
    37 
    38     while(!q.empty()){
    39         cur=q.front();
    40         q.pop();
    41         for(i=0;i<edge[cur].size();i++){//只让下面还有节点的点入队,不让叶结点入队
    42             if(edge[edge[cur][i]].size()){
    43                 q.push(edge[cur][i]);
    44                 last=edge[cur][i];
    45             }
    46             else{
    47                 count++;//计叶结点数
    48             }
    49         }
    50         //cout<<"cur:  "<<cur<<endl;
    51         if(e==cur){
    52             e=last;
    53             price*=1+r;
    54             maxcount=count;
    55 
    56             //cout<<cur<<" "<<e<<" "<<price<<endl;
    57 
    58             count=0;
    59         }
    60     }
    61     printf("%.2lf %d
    ",price,maxcount);
    62     return 0;
    63 }
  • 相关阅读:
    一个int类型究竟占多少个字节
    TCMalloc 安装与使用
    [创意标题] spoj 11354 Amusing numbers
    如何更好地理解和使用Github
    一天JavaScript示例-点击图片显示大图片添加鼠标
    php方法综述除去换行符(PHP_EOL使用变量)
    使用jQuery和css3实现了仿淘宝ued博客左边的菜单切换动画
    【软件使用技巧】PL/SQL Developer实现双击table询
    newlisp 接受jenkins带空格的参数
    Oracle listener lsnrctl
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4786367.html
Copyright © 2011-2022 走看看