1090. Highest Price in Supply Chain (25) -计层的BFS改进
题目如下:
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改进的更多相关文章
- [建树(非二叉树)] 1090. Highest Price in Supply Chain (25)
1090. Highest Price in Supply Chain (25) 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)(25 分)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)–everyone inv ...
- 1079. Total Sales of Supply Chain (25) -记录层的BFS改进
题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...
- PAT (Advanced Level) 1090. Highest Price in Supply Chain (25)
简单dfs. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- 1090. Highest Price in Supply Chain (25)-dfs求层数
给出一棵树,在树根出货物的价格为p,然后每往下一层,价格增加r%,求所有叶子节点中的最高价格,以及该层叶子结点个数. #include <iostream> #include <cs ...
- 【PAT甲级】1090 Highest Price in Supply Chain (25 分)
题意: 输入一个正整数N(<=1e5),和两个小数r和f,表示树的结点总数和商品的原价以及每向下一层价格升高的幅度.下一行输入N个结点的父结点,-1表示为根节点.输出最深的叶子结点处购买商品的价 ...
- pat1090. Highest Price in Supply Chain (25)
1090. Highest Price in Supply Chain (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 C ...
随机推荐
- hdu3183 RMQ
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Java文件操作(新建,遍历,删除)
//创建文件 private static void createFile(File file){ try { if(!file.exists()){ if(file.getParentFile() ...
- Maven之自定义archetype生成项目骨架
Maven之自定义archetype生成项目骨架(一) http://blog.csdn.net/sxdtzhaoxinguo/article/details/46895013
- vmware虚拟机CentOS7安装oracle数据库
想用linux虚拟机装一个oracle,中间遇到的坑太多了,最后总算是安装好了,一定要写个全面的教程出来. 话不多说 通用编辑命令: vi test.txt #进入编辑模式 编辑完成后按ESC退出 ...
- js error
0x800a0259 - JavaScript 运行时错误: 未知的运行时错误 <p id="navigatorInfo"></p> var txt = & ...
- 总结angular+ionic项目中的问题
1:tab的路由导向问题 运用ion-tabs时,第一个ion-tabs标签下的href功能会覆盖掉路由中定义的默认路由(进入应用后直接加载href指向的组件). 解决方法:多写一个ion-tabs标 ...
- 初识Redis系列之一:简单介绍
一:Redis是什么? Redis全称:REmote DIctionary Server(Redis) .Redis是一个由Salvatore Sanfilippo写的key-value存储系统,AN ...
- 2. struct A 和 typedef struct A
2. struct A 和 typedef struct A 2.1 struct A struct A{}定义一个名为struct A的结构体. 下例定义了struct A同时,声明了两个变量(注意 ...
- springMVC源码分析--ViewNameMethodReturnValueHandler返回值处理器(三)
之前两篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)和springMVC源码分析--HandlerMethodReturnValu ...
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个Spring Boot是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...