Highest Price in Supply Chain (25)

2022/8/29 6:25:10

本文主要是介绍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.

输入描述:

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 }

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



这篇关于Highest Price in Supply Chain (25)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程