ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Highest Price in Supply Chain (25)

2022-08-29 00:30:20  阅读:209  来源: 互联网

标签:25 dist chain int Price number supplier Highest price


题目描述

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.

输入描述:

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.

输出描述:

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.

输入例子:

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

输出例子:

1.85 2
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int dist[100005]={0};
 4 int number[100005];
 5 int main(){
 6     int n,root;
 7     float p,r;
 8     cin>>n>>p>>r;
 9     for(int i=0;i<n;++i){
10         cin>>number[i];
11     }
12     //对每一个结点进行回溯
13     for (int i=0;i<n;++i){
14         int temp=i;
15         while(number[temp]!=-1){
16             if (dist[number[temp]]>0){ //剪枝,如果该结点的父节点已确定高度则直接使用
17                 dist[i]+=dist[number[temp]]+1;
18                 break;
19             }
20             else{
21                 dist[i]++;
22                 temp=number[temp];
23             }
24         }
25     }
26     
27     int mmax=-1;
28     int count;
29     for (int i=0;i<n;++i){
30         if (mmax==dist[i]){
31             count++;
32         }
33         else if (mmax<dist[i]){
34             mmax=dist[i];
35             count=1;
36         }
37     }
38 
39     printf("%.2f %d",p*pow((100+r)/100,mmax),count);
40 }

题目乍一看不太容易理解,但本质上是一道通过回溯求叶子结点深度的问题,没啥细节,直接回溯就行了

标签:25,dist,chain,int,Price,number,supplier,Highest,price
来源: https://www.cnblogs.com/coderhrz/p/16634569.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有