1090. Highest Price in Supply Chain (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

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

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

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

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

  3. PAT 1090 Highest Price in Supply Chain[较简单]

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

  4. 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 ...

  5. 1090 Highest Price in Supply Chain——PAT甲级真题

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

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

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

  7. 1090. Highest Price in Supply Chain (25) -计层的BFS改进

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

  8. A1090. Highest Price in Supply Chain

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

  9. PAT 甲级 1090 Highest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...

随机推荐

  1. 集群通信组件tribes之集群的消息接收通道

    与消息发送通道对应,发送的消息需要一个接收端接收消息,它就是ChannelReceiver.接收端负责接收处理其他节点从消息发送通道发送过来的消息,实际情况如图每个节点都有一个ChannelSende ...

  2. Weka 算法大全

    关联规则挖掘 (一)  Apriori (二)  FilteredAssociator (三)  FPGrowth (四)  GeneralizedSequentislPatterns (五)  Pr ...

  3. 《java入门第一季》之面向对象(构造方法)

    /* 构造方法: 给对象的数据进行初始化 格式: A:方法名与类名相同 B:没有返回值类型,连void都没有 C:没有具体的返回值 */ class Student { private String ...

  4. "《算法导论》之‘图’":深度优先搜索、宽度优先搜索(无向图、有向图)

    本文兼参考自<算法导论>及<算法>. 以前一直不能够理解深度优先搜索和广度优先搜索,总是很怕去碰它们,但经过阅读上边提到的两本书,豁然开朗,马上就能理解得更进一步. 下文将会用 ...

  5. HBase rest

    HBase Rest 是建立在HBase java 客户端基础之上的,提供的web 服务.它存在的目的是给开发者一个更多的选择. 1.启动rest 服务 (1)hbase rest start 用默认 ...

  6. zookeeper 应用开发

    由于zookeeper的client只有zookeeper一个对象,使用也比较简单,所以就不许要文字说明了,在代码中注释下就ok 了. 1.测试用的main方法 package ClientExamp ...

  7. Http的定义及其基本概念介绍

    HTTP的特性 HTTP构建于TCP/IP协议之上,默认端口号是80 HTTP是无连接无状态的 HTTP报文 请求报文 HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST ...

  8. OS X10.10下HomeBrew的安装提示

    apple@kissAir: c_src$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...

  9. 关于gcc的一点小人性化提示

    现在对于大多数平台的C编译器来说都会有很多种选择,而gcc和clang无疑是2个非常优秀的C编译器.当然他们也不只是C编译器.我最近用clang的比较多,原因有很多.不过一些小的细节很让我喜欢,比如O ...

  10. JavaScript,只有你想不到

    很长时间以来,JavaScript在我眼里都是编程语言中的二等公民.早先,它经常是很多安全问题的发源地,就像是胶水一样,它能把HTML应用与样式 粘到一块,可没有人拿它来正正规规地编写程序:这样的情形 ...