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 ...
随机推荐
- 2016/1/9:深度剖析安卓Framebuffer设备驱动
忙了几天,今天在公司居然没什么活干 ,所以早上就用公司的电脑写写之前在公司编写framebuffer的使用心得体会总结,这也算是一点开发经验,不过我还没写全,精华部分还是自己藏着吧.直到下午才开始有点 ...
- Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
Android NFC技术(三)--初次开发Android NFC你须知道NdefMessage和NdefRecord 这最近也是有好多天没写博客了,除了到处张罗着搬家之外,依旧还是许许多多的琐事阻碍 ...
- 苹果新的编程语言 Swift 语言进阶(七)--枚举、结构、类
Swift语言中,具有类特征的类型包括三种,即枚举类型.结构类型(包括基本类型,基本类型实际都是结构类型的特例).类.其中枚举类型.结构类型是属于值类型,类属于引用类型.三种类型都可以添加属性.方法. ...
- Java学习网站大全
Java学习网站 http://www-900.ibm.com/developerWorks/cn/java/index.shtml IBM的JAVA专题--永远的蓝色巨人 http://www.hu ...
- HBase数据字典
数据字典用来存储了系统的元数据.HBase的元数据包括:用户表的定义.表的切分方案.分片的分布情况(即分片分布在哪个regionserver上).分片对应的数据文件和日志文件.其中,分片和数据文件的映 ...
- 关于SharePoint2007简单随感
首先,还是要感谢我毕业以后的这第一份正式工作,当然现在也依然在做,带我走进了SharePoint的世界,很奇妙也许是有缘吧,自己不是个努力的人,从面试的时候对Moss这个东西闻所未闻,到现在一知半解, ...
- java基础多线程之共享数据
java基础巩固笔记5-多线程之共享数据 线程范围内共享数据 ThreadLocal类 多线程访问共享数据 几种方式 本文主要总结线程共享数据的相关知识,主要包括两方面:一是某个线程内如何共享数据,保 ...
- C语言高级应用---操作linux下V4L2摄像头应用程序
我们都知道,想要驱动linux下的摄像头,其实很简单,照着V4L2的手册一步步来写,很快就可以写出来,但是在写之前我们要注意改变系统的一些配置,使系统支持framebuffer,在dev下产生fb0这 ...
- rails将类常量重构到数据库对应的表中之一
问题是这样:原来代码.html.erb页面中有一个select元素,其每个item对应的是model中的类常量: <%= f.select :pay_type,Order::PAYMENT_TY ...
- Unity的资源管理
本篇文章翻译自Unity的官方文档,原文地址:https://docs.unity3d.com/Manual/BehindtheScenes.html Unity自动导入资源的同时为你管理这些资源产生 ...