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. Only the retailers will face the customers. 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 lowest price a customer 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 (≤10​5​​), the total number of the members in the supply chain (and hence their ID's are numbered from 0 to N−1, and the root supplier's ID is 0); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:

K​i​​ ID[1] ID[2] ... ID[K​i​​]

where in the i-th line, K​i​​ is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID's of these distributors or retailers. K​j​​ being 0 means that the j-th member is a retailer. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 10​10​​.

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
0
0
0

Sample Output:

1.8362 2

 #include <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
using namespace std;
const int maxn = ;
int n;
double p,r;
vector<int> res[maxn];
int num=,lvl=;
void bfs(int root){
queue<int> q;
q.push(root);
int flag=;
while(!q.empty()){
queue<int> tmp;
while(!q.empty()){
int now = q.front();
q.pop();
if(res[now].empty()){
num++;
flag=;
}
for(int i=;i<res[now].size();i++){
tmp.push(res[now][i]);
}
}
if(flag)break;
while(!tmp.empty()){
q.push(tmp.front());
tmp.pop();
}
lvl++;
}
}
int main(){
scanf("%d %lf %lf",&n,&p,&r);
int i;
for(i=;i<n;i++){
int k;
scanf("%d",&k);
for(int j=;j<k;j++){
int x;
scanf("%d",&x);
res[i].push_back(x);
}
}
bfs();
p=p*pow((+r/),lvl);
//printf("%d",lvl);
printf("%.4f %d",p,num);
}

注意点:bfs遍历要一层一层保存和入队,用正常的一个队列进行bfs会出现不该入队的元素入队,导致结果错误。例如原本第二层会到达终点,但队列里的第二层元素不是第一个元素就到达终点了,而是后面几个,这样第一个元素的子节点也已经入队,此时如果子节点也到了终点就会多算。

PAT A1106 Lowest Price in Supply Chain (25 分)——树的bfs遍历的更多相关文章

  1. 【PAT甲级】1106 Lowest Price in Supply Chain (25分)

    题意:输入一个正整数N(<=1e5),两个小数P和R,分别表示树的结点个数和商品原价以及每下探一层会涨幅的百分比.输出叶子结点深度最小的商品价格和深度最小的叶子结点个数. trick: 测试点1 ...

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

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

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

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

  4. PAT甲级——A1106 Lowest Price in Supply Chain

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

  5. PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...

  6. 1106. Lowest Price in Supply Chain (25)

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

  7. A1106. Lowest Price in Supply Chain

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

  8. PAT 1106 Lowest Price in Supply Chain

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

  9. PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)

    简单dfs #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

随机推荐

  1. 汇编语言--CPU资源和存储器(二)

    二.CPU资源和存储器 需要访问的硬件资源主要有:CPU内部资源.存储器和I/O端口. 1.寄存器组 (1)16位寄存器组 16位CPU所含有的寄存器有(见图2.1中16位寄存器部分): 4个数据寄存 ...

  2. javasript中var、let和const区别

    let和const都是es5,es6新版本的js语言规范出来的定义,在这以前定义一个变量只能用var.let和const都是为了弥补var的一些缺陷而新设计出来的. 简单来说是: let是修复了var ...

  3. 不固定个数组,进行一一对应的组合,js将多个数组实现排列组合

    var arr = [ ["a", "b"], ["1", "2"], ["d"] ]; var s ...

  4. Spider-one

    1. 爬虫是如何采集网页数据的: 网页的三大特征: -1. 每个网页都有自己的 URL(统一资源定位符)地址来进行网络定位. -2. 每个网页都使用 HTML(超文本标记语言)来描述页面信息. -3. ...

  5. [简记] fetch API 的初步使用

    var myHeaders = new Headers(); myHeaders.append('Content-Type', 'application/x-www-form-urlencoded; ...

  6. 【代码笔记】Web-ionic-Range

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  7. Leaflet 测试加载高德地图

    <!DOCTYPE html> <html> <head>  <title>Leaflet Quick Start Guide Example</ ...

  8. Linux 学习笔记之超详细基础linux命令 Part 6

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 5----------------- ...

  9. genymotion 模拟器内安装软件 the app contains ARM native code and your devices cannot run ARM instructions

    问题如图: 解决方法: 下载一个Genymotion-ARM-Translation软件,安装到模拟器中就好了

  10. mysql初次启动相关配置

    一.启动mysql服务 使用mysql之前,首先要启动mysql服务器端.使用命令mysqld.在命令行终端先进入mysql安装的目录直到bin目录,如图: 运行这个命令,可能会报错,提示找不到dat ...