Highest Price in Supply Chain (25)
Posted gn1314
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Highest Price in Supply Chain (25)相关的知识,希望对你有一定的参考价值。
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 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 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 ",maxn,cnt); 44 45 return 0; 46 }
以上是关于Highest Price in Supply Chain (25)的主要内容,如果未能解决你的问题,请参考以下文章
1090. Highest Price in Supply Chain (25)
1090 Highest Price in Supply Chain (25 分)
PAT 1090. Highest Price in Supply Chain
1090 Highest Price in Supply Chain (25 分)