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\n",price,maxcount);
62     return 0;
63 }

 

posted @ 2015-09-06 16:19  Deribs4  阅读(251)  评论(0编辑  收藏  举报