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 ...
随机推荐
- bzoj 4870: [Shoi2017]组合数问题
Description Solution 考虑这个式子的组合意义: 从 \(n*k\) 个球中取若干个球,使得球的数量 \(\%k=r\) 的方案数 可以转化为 \(DP\) 模型,设 \(f[i][ ...
- Go学习——defer、panic
defer: 延迟到ret之前,通常用于IO的关闭 or 错误处理. 在延迟出现的异常可以被后面的捕捉,但是只有最后一个. defer可以多次,这样形成一个defer栈,后defer的语句在函数返回时 ...
- UVA - 11997:K Smallest Sums
多路归并 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> ...
- HDU2303(数论)大整数求余+素数筛选
Sample Input 143 10 143 20 667 20 667 30 2573 30 2573 40 0 0 Sample Output GOOD BAD 11 GOOD BAD 23 ...
- 【hihoCoder 1419】重复旋律4
Description 小 Hi 平时的一大兴趣爱好就是演奏钢琴. 我们知道一个音乐旋律被表示为长度为 N的数构成的数列. 小 Hi 在练习过很多曲子以后发现很多作品中的旋律有重复的部分. 我们把一段 ...
- day4 liaoxuefeng--调试、线程、正则表达式
一.错误.调试和测试 二.进程和线程 三.正则表达式
- 网易笔试题:浏览器中输入一个url后回车到返回页面信息的过程
You enter a URL into the browser输入一个url地址 The browser looks up the IP address for the domain name浏览器 ...
- 关于一些基础的Java问题的解答(二)
6. Hashcode的作用 官方对于hashCode的解释如下: Whenever it is invoked on the same object more than once during an ...
- Docker 第一篇 认识Docker 的作用好处
Docker 第一篇 认识Docker 的作用好处 (1)什么是Docker (2)Docker 优势劣势 Docker是去年开始关注并学习的,因为项目用到了AspnetCore 了解了之后总感觉会用 ...
- Codeforces Round #396(Div. 2) A. Mahmoud and Longest Uncommon Subsequence
[题意概述] 找两个字符串的最长不公共子串. [题目分析] 两个字符串的最长不公共子串就应该是其中一个字符串本身,那么判断两个字符串是否相等,如果相等,那么肯定没有公共子串,输出"-1&qu ...