码迷,mamicode.com
首页 > 其他好文 > 详细

Highest Price in Supply Chain (25)

时间:2020-01-29 14:23:02      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:lin   double   改变   The   保存   name   oid   ble   sub   

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 S?i?? is the index of the supplier for the i-th member. S?root?? 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


思路:1.拿到题目的第一想法是并查集,并且已经写好了,但是发现并查集会改变路径,也就是会改变结点的层次,因为并的过程是x,y的根结点并的,而本题是x,y直接相连接
   2.由于题目没有形成环,也没有一个点有多个父节点,所以可以直接用数组arr保存父节点,然后直接往上推
   3.当然也可以用建立图来写,但是我认为这个可以跟并查集有个较好的比较就这么写了

代码如下:
 1 #include <cstdio>
 2 using namespace std;
 3 
 4 double p,r;
 5 int n,arr[100005];
 6 double ans[100005];
 7 
 8 double get(int x){
 9     int r1=x;
10     double res=p;
11     while(r1!=arr[r1]){
12         r1=arr[r1];
13         if(r1==-1)
14             break;
15         res=res+(res*0.01*r);
16     }
17     if(r1==-1)
18         return res;
19 
20     else
21         return -1;
22 }
23 
24 int main(void)
25 {
26     scanf("%d%lf%lf",&n,&p,&r);
27     for(int i=0;i<n;i++)
28         arr[i]=i;
29     for(int i=0;i<n;i++)
30         scanf("%d",&arr[i]);
31 
32     int cnt=0;
33     double maxn=0;
34     for(int i=0;i<n;i++){
35         ans[i]=get(i);
36         if(ans[i]>maxn)
37             maxn=ans[i];
38     }
39     for(int i=0;i<n;i++){
40         if(maxn-ans[i]<=0.00000000001)
41             cnt++;
42     }
43     printf("%.2lf %d\n",maxn,cnt);
44 
45     return 0;
46 }

 




Highest Price in Supply Chain (25)

标签:lin   double   改变   The   保存   name   oid   ble   sub   

原文地址:https://www.cnblogs.com/gn1314/p/12239997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!