PAT1090:Highest Price in Supply Chain
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 思路
这道题归根结底就是用dfs求图(或者说树)的根节点到终点(或者说树的叶节点)最大路径maxLength,那么利润的增长次数就是maxLength - 1,此时价格最大。那么步骤可以是: 1.根据输入构造图
2.dfs找最大路径maxlength,并统计最大路径的次数。
3.最大价格 = price * pow( 1 + rate , maxLength - 1)
4.输出最大价格(两位小数)和最大路径的出现次数。 代码
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std; vector<int> levels(,); //dfs and count level's num
void dfs(const vector<vector<int>>& graph,int root,int level)
{
levels[level]++;
if(graph[root].empty())
return;
for(int i = ;i < graph[root].size();i++)
dfs(graph,graph[root][i],level + );
} int main()
{
int N;
double price,rate;
while(cin >> N >> price >> rate)
{
vector<vector<int>> graph(N);
int root = ;
//build graph
for(int i = ;i < N;i++)
{
int father;
cin >> father;
if(father == - )
root = i;
else
graph[father].push_back(i);
} //dfs
dfs(graph,root,); //find the maximum depth
int maxLength = ;
for(int i = ; i <levels.size();i++)
{
if(levels[i] == )
{
maxLength = i - ;
break;
}
}
//calculate price
for(int i = ;i < maxLength - ;i++)
{
price *= ( + rate/);
}
cout << fixed << setprecision() << price << " ";
cout << levels[maxLength] << endl;
levels.clear();
}
}
PAT1090:Highest Price in Supply Chain的更多相关文章
- pat1090. Highest Price in Supply Chain (25)
1090. Highest Price in Supply Chain (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 C ...
- [建树(非二叉树)] 1090. Highest Price in Supply Chain (25)
1090. Highest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors ...
- PAT 1090 Highest Price in Supply Chain[较简单]
1090 Highest Price in Supply Chain(25 分) A supply chain is a network of retailers(零售商), distributors ...
- PAT_A1090#Highest Price in Supply Chain
Source: PAT A1090 Highest Price in Supply Chain (25 分) Description: A supply chain is a network of r ...
- 1090 Highest Price in Supply Chain——PAT甲级真题
1090 Highest Price in Supply Chain A supply chain is a network of retailers(零售商), distributors(经销商), ...
- 1090. Highest Price in Supply Chain (25)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...
- 1090. Highest Price in Supply Chain (25) -计层的BFS改进
题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...
- A1090. Highest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT 甲级 1090 Highest Price in Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...
随机推荐
- Understanding Android Security(安卓安全的理解)
论文作者: Enck, William Ongtang, MacHigar McDaniel, Patrick 下一代的开放操作系统不会在个人主机和大型主机上出现,而是在只能手机上.新环境的开放性将会 ...
- FFMPEG结构体分析:AVIOContext
注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrame FFMPEG结构体分析:AVFormatContext FFMPEG结构体分析:AVCodecConte ...
- C/C++语言中NULL、'\0’和0的区别
注:本文参考了http://blog.csdn.net/mylinx/article/details/6873253及书籍<征服C指针>([日]前桥和弥著). NULL.'\0'和0的值是 ...
- 客户全局信用控制&非全局信用控制
看个简单的例子 客户信用限额 非全局信用控制 非全局信用控制比较简单,我们看一下全局信用控制 设置: 实现结果:全局&非全局对比
- mybati源码之ReuseExecutor
/** * @author Clinton Begin */ public class ReuseExecutor extends BaseExecutor { private final Map&l ...
- 简单开发Apple Watch的步骤
好久没写博客了,自己这两年自从孩子出世,也慢慢懈怠了.实在有点对不住了,换了个新公司,也有点时间可以写写东西了. 前几天苹果刚刚发布Apple Watch,Xcode6也更新了watchKit,正好 ...
- 恶补web之五:dhtml学习
dhtml是一种使html页面具有动态特性的艺术.对于多数人来说dhtml意味着html(html DOM),样式表和javascript的组合. dhtml不是w3c标准.dhtml指动态html, ...
- C# 合并多种格式文件为PDF
文档合并是一种高效文档处理方式.如果能够有一个方法能将多种不同类型的文档合并成一种文档格式,那么在文档存储管理上将为我们提供极大的便利.因此,本篇文章介绍了一种如何使用免费组件Free Spire.O ...
- MOOS学习笔记4——独立线程不同回调
MOOS学习笔记4--独立线程不同回调 /** * @fn 独立线程不同回调 * @version v1.0 * @author */ #include "MOOS/libMOOS/Comm ...
- partition List(划分链表)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...