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 }
  • 相关阅读:
    hihocoder 1489(微软2017, 数学,模拟)
    图论——迪杰斯特拉算法(Dijkstra)实现,leetcode
    DFS,BFS 练习(深搜,广搜,图,leetcode)
    什么是渗透测试?黑客安全专家郭盛华这样说
    为什么印度容易遭受网络黑客攻击?
    郭盛华年收入5000万是真的吗?
    警方突袭德国间谍软件公司
    苹果推出首款5G手机,相机功能比单反还要牛?
    苹果推出iPhone 12,价格比你想象中更实惠
    韩国AI半导体技术,为何能问鼎世界第一?
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4786367.html
Copyright © 2011-2022 走看看