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 ...
随机推荐
- visual svn使用教程
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什 ...
- android 线程那点事
在操作系统中,线程是操作系统调度的最小单元,同时线程又是一种受限的系统资源,即线程不可能无限制的产生,并且线程的创建和销毁都会有相应的开销,当系统中存在大量的线程时,系统会通过时间片轮转的方式调度每个 ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- java中split(regex)使用中要注意的问题:正则表达式
比如我在项目中遇到的(,),.,|,*等等类的符号: String area="(30.13206313822174, 120.4156494140625)(29.8763738070713 ...
- 直接执行SQL语句的快捷键是什么啊?嘎嘎
在查询中输入SQL语句后,执行语句的快捷键~ 分享到: 2009-10-23 10:59网友采纳 左键..嘿嘿,开个玩笑 你把鼠标移动到执行按钮上停一会就能看到了啊
- Windows上模拟Linux环境的软件Cygwin
Windows上模拟Linux环境的软件Cygwin 2010-10-11 15:19 我要评论(0) 字号:T|T Cygwin是一个用于在Windows上 模拟Linux环境的软件.它可 ...
- windows下mongodb安装详解
1.打开官网https://www.mongodb.com/download-center?jmp=nav#community 注:这里小伙伴们可是开启下FQ软件psiphon 3下载(不开启FQ好像 ...
- windows环境下zookeeper安装和使用
一.简介 zooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一 ...
- Charles手机抓包实用教程
一.Charles官网下载链接:https://www.charlesproxy.com/download/ 二.抓包步骤: 1.安装Charles,并打开 2.电脑设置代理端口:打开charles- ...
- window.open open new window?
when ever i use window.location.href=//some url it always open a new window, this only happens when ...