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.

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

题目大意:给一棵树,在树根处货物的价格为p,然后每往下走一层,价格增加r%,求所有叶子结点的最高价格,以及这个价格的叶子结点个数。

知识点:

深度优先搜索的思想

Dfs思修基于递归思想,通过递归的形式来缩小问题规模,把一件事分割成若干个相同的小事,逐步完成。

深度优先搜索的步骤分为 1.递归下去 2.回溯上来。顾名思义,深度优先,则是以深度为准则,先一条路走到底,直到达到目标。这里称之为递归下去。

否则既没有达到目标又无路可走了,那么则退回到上一步的状态,走其他路。这便是回溯上来。

递归的使用条件:

存在一个递归调用的终止条件;每次递归的调用必须越来越靠近这个条件;只有这样递归才会终止,否则是不能使用递归的!

递归的基本原理:
1每一次函数调用都会有一次返回.当程序 执行到某一级递归的结尾处时,它会转移到前一级递归继续执行.

2递归函数中,位于递归调用前的语句和各级被调函数具有相同的顺序.如打印语句 #1 位于递归调用语句前,它按照递归调用的顺序被执行了 4 次.

3每一级的函数调用都有自己的局部变量.

4递归函数中,位于递归调用语句后的语句的执行顺序和各个被调用函数的顺序相反.
 即位于递归函数入口前的语句,从外往里执行;位于递归函数入口后面的语句,由里往外执行。

5虽然每一级递归有自己的变量,但是函数代码并不会得到复制.

6递归函数中必须包含可以终止递归调用的语句。

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int n, temp,root,maxdepth=0,maxnum=0;
vector<int>v[100010];
void dfs(int index,int depth){
    if(v[index].size()==0){
        if(maxdepth==depth){
            maxnum++;
        }
        if(maxdepth<depth){
            maxdepth=depth;
            maxnum=1;
        }
        return;
    }
    for(int j=0;j<v[index].size();j++)
        dfs(v[index][j],depth+1);
}

int main()
{
 
  double p,r;
    scanf("%d %lf %lf",&n,&p,&r);
    for(int i=0;i<n;i++){
        scanf("%d",&temp);
        if(temp==-1) root=i;
        else v[temp].push_back(i);//v[temp]存储其子节点,temp为父节点下标,i为子节点下标include
    }
    dfs(root,0);
    printf("%.2f %d",p*pow(1+r/100,maxdepth),maxnum);// double pow(double x, double y)返回x的y次幂,即xy。
    

  return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值