题目如下:

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

题目要求根据供应链条计算最深层的零售商的售价,实质是图的BFS,并且BFS要记录层级,这道题和之前的一道1079. Total Sales of Supply Chain (25)算法一致,这里不再赘述。唯一的不同是图的输入,并且根结点不再固定是0。

代码如下:

#include <iostream>
#include <vector>
#include <stdio.h>
#include <queue>
#include <math.h> using namespace std; int main()
{
vector<vector<int> > graph;
int N;
double P,r;
cin >> N >> P >> r;
graph.resize(N);
int num;
int root;
for(int i = 0; i < N; i++){
scanf("%d",&num);
if(num == -1){
root = i;
continue;
}
graph[num].push_back(i);
}
queue<int> q;
q.push(root);
int level = 0;
int last,tail;
last = tail = root;
int maxLevel = 0;
int cnt = 0;
while(!q.empty()){
int v = q.front();
q.pop();
if(level == maxLevel){
cnt++;
}else if(level > maxLevel){
maxLevel = level;
cnt = 1;
}
for(int i = 0; i < graph[v].size(); i++){
int w = graph[v][i];
q.push(w);
tail = w;
}
//printf("(%d)->%d\n",level,v);
if(v == last){
last = tail;
level++;
if(level > 1) P*= (100+r)/100;
} }
printf("%.2lf %d\n",P,cnt); return 0;
}

1090. Highest Price in Supply Chain (25) -计层的BFS改进的更多相关文章

  1. [建树(非二叉树)] 1090. Highest Price in Supply Chain (25)

    1090. Highest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors ...

  2. 1090. Highest Price in Supply Chain (25)

    时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  3. 1090 Highest Price in Supply Chain (25)(25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  4. PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)–everyone inv ...

  5. 1079. Total Sales of Supply Chain (25) -记录层的BFS改进

    题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...

  6. PAT (Advanced Level) 1090. Highest Price in Supply Chain (25)

    简单dfs. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  7. 1090. Highest Price in Supply Chain (25)-dfs求层数

    给出一棵树,在树根出货物的价格为p,然后每往下一层,价格增加r%,求所有叶子节点中的最高价格,以及该层叶子结点个数. #include <iostream> #include <cs ...

  8. 【PAT甲级】1090 Highest Price in Supply Chain (25 分)

    题意: 输入一个正整数N(<=1e5),和两个小数r和f,表示树的结点总数和商品的原价以及每向下一层价格升高的幅度.下一行输入N个结点的父结点,-1表示为根节点.输出最深的叶子结点处购买商品的价 ...

  9. pat1090. Highest Price in Supply Chain (25)

    1090. Highest Price in Supply Chain (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 C ...

随机推荐

  1. [SDOI2009]学校食堂Dining

    题目描述 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴.当然,不同的人口味也不一定相同,但每个人的口味都可以用一个非负整数 ...

  2. bzoj4361isn 容斥+bit优化dp

    4361: isn Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 375  Solved: 186[Submit][Status][Discuss] ...

  3. C语言程序设计第二次作业——顺序结构

    (一)改错题 1.输出带框文字:在屏幕上输出以下3行信息. 错误信息1: 错误原因:i和d位置错误 改正方法:i和d位置互换 错误信息2: 错误原因:\n后缺了一个" 改正方法:\n后加一个 ...

  4. 如何在Linux上编译c++文件

    1. 打开Linux客户端,新建一个c++文件 2. 写如下代码,退出保存 3.对.cpp文件进行编译并输出结果.

  5. mongo索引

    索引自动创建和手工创建 db.stu.drop(); db.stu.insert({"name":"张三","sex":"男&qu ...

  6. Apache 443端口占用解决方法

    当运行httpd.exe时,出现如下问题 原因是启动Apache会占用443端口,而443被其他程序占用了.我们只需将Apache默认端口443改掉就行.网上搜了一下,说是更改Apache24\con ...

  7. About Windows 10 April 2018 Update

    在四月的最后一天,微软终于正式发布了 Windows 10 的又一次重大更新,并命名为 Windows 10 四月更新,轮压哨,我软确实谁也不服:再晚一天,我软改名部门恐怕又要发挥作用了,毕竟我软存在 ...

  8. PTA中提交Python3程序的一些套路

    0. FAQ 0.1 提交后提示"答案错误"或者"格式错误" PTA检查答案正确与否是通过字符串匹配实现的.所以可能有以下几种原因: 格式错误:程序的输出要与题 ...

  9. 在Spring Boot中输出REST资源

    前面我们我们已经看了Spring Boot中的很多知识点了,也见识到Spring Boot带给我们的各种便利了,今天我们来看看针对在Spring Boot中输出REST资源这一需求,Spring Bo ...

  10. 优先使用TimeUnit类中的sleep()

    TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,TimeUnit提供了可读性更好的线程暂停操作,通常用来替换Thread.sleep(),在很长一段 ...