1090. Highest Price in Supply Chain (25)

来源:互联网 发布:wan端口 编辑:程序博客网 时间:2024/04/29 13:25

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 (<=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


结题思路:

为每个下游链的供应商不断得寻找他的上家,同时记录跳转的次数,比较得到需要搜索的最多跳转次数。递归部分如果不记录在搜索中得到的中间供应商距离rootsupply的距离将会在第三个case的时候超时。

#include<iostream>#include<string>#include<cstring>#include<cstdlib>#include<cmath> using namespace std;int pre[100005];//记录每个下家的上家 int distanceFromRoot[100005];//记录每个下家距离rootSupply的距离 int distanceToRoot(int site){    if(distanceFromRoot[site]!=-2)        return distanceFromRoot[site];    distanceFromRoot[site]=distanceToRoot(pre[site])+1;//加快递归的进行,以后在遇到相同的supply,直接返回距离即可     return distanceFromRoot[site]; } int MaxDistanceToRoot(int num)//找到距离rootSupply最远的下家  {     int maxDistance=-1;     for(int i=0;i<num;++i)     {         distanceFromRoot[i]=distanceToRoot(i);         if(distanceFromRoot[i]>maxDistance)            maxDistance=distanceFromRoot[i];     }     return maxDistance;}int main(){    int num,root;    double orignalP,rate;    cin>>num>>orignalP>>rate;    for(int i=0;i<num;++i)    {        cin>>pre[i];        if(pre[i]==-1)           root=i;        distanceFromRoot[i]=-2;    }    distanceFromRoot[root]=0;//初始化     int times=0;    int maxDistance=MaxDistanceToRoot(num,times);    printf("%.2f ",orignalP*pow(1+rate/100,maxDistance));    for(int i=0;i<num;++i)        if(distanceFromRoot[i]==maxDistance)           ++times;     cout<<times<<endl;    system("pause");    return 0;} 
0 0
原创粉丝点击