【PAT】1090. 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.

翻译:一个供货链是由一张零售商和经销商和供应商组成的网络——从供应商到客户之间每个人都紧紧相连。
从一个总供应商开始,每个在链上的人从各自的供应商上以P的价格买产品并以r%的利润卖给或批给其他人。只有零售商将会直面消费者。假设除了总供应商之外每个供应链上的成员都有一个供应商,并且没有供应环。
现在给你一个供应链,你需要说出我们可以预测的一些零售商的最高价格。

INPUT FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括三个正整数:N(<=10^5),供应链的总人数(因此他们的ID被标记为从0-N-1,并且总供应商的ID是0);P,代表总供应商的原价;r,代表每个经销商或零售商的利率。接着下一行包括N个数字,每个数字Si为第i个人的供应商编号。总供应商被定义为-1。所有数字之间用空格隔开。

OUTPUT FORMAT

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.

翻译:对于每组输入数据,输出一行我们可以预测的一些零售商的最高价格,保留2位小数,和卖最高价格的零售商数。两个数字之间必须用空格隔开。数据保证价格不超过10^10.


Sample Input:

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

Sample Output:

1.85 2


解题思路

通过深度优先搜索计算出最长的供应链,再定义一个计数器计算出这个供应链的个数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
double P,r;
int v[100010];
int fa[100010];
int Max=0,ccount=0;
int dfs(int a){
    int temp=v[a];
    if(fa[temp])fa[a]=fa[temp]+1;
    else fa[a]=dfs(temp)+1;
    return fa[a];
}
int main(){
    scanf("%d%lf%lf",&N,&P,&r);
    for(int i=0;i<N;i++){
        scanf("%d",&v[i]);
        if(v[i]<0)fa[i]=1; 
    }
    for(int i=0;i<N;i++){
        if(!fa[i])dfs(i);
        if(Max<fa[i])Max=fa[i],ccount=1;
        else if(Max==fa[i])ccount++;
    }
    double ans=P*pow(1.0+r/100,Max-1);
    printf("%.2lf %d\n",ans,ccount);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/tjj1998/article/details/80240337