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. 数据库编程Case when

    数据库编程题 1. 姓名 日期 是否上班 张三 星期二 是 张三 星期三 是 李四 星期一 是 王五 星期二 是 张三 星期二 是 写出一条SQL语句输出下列结果 姓名 星期一 星期二 星期三 张三 ...

  2. HibernateTemplate的用法以及作用

    HibernateTemplate作用:从字面上意思我们就知道他是一个模板,然后我们又知道hibernate是一个对象关系映射的框架,所以我们很容易联想到他的功能就是将Hibernate 的持久层访问 ...

  3. php7.27: export excel from mysql

    https://stackoverflow.com/questions/15699301/export-mysql-data-to-excel-in-php https://github.com/PH ...

  4. loadrunner 脚本优化-关联函数web_reg_save_param()函数详解

    脚本优化-关联函数web_reg_save_param()函数详解   by:授客 QQ:1033553122 Insert->New Step,打开Add Step对话框 选择函数web_re ...

  5. Android视屏播放兼容性问题分享

    最近产品提了一个紧急需求:webview加载的URL,需要支持视频播放. 为了快速完成需求,功能实现上直接使用系统自带播放器播放视频.由于是自带播放器,需要进行兼容性测试,过程发现了不少问题,这里分享 ...

  6. K邻近分类算法

    # -*- coding: utf-8 -*- """ Created on Thu Jun 28 17:16:19 2018 @author: zhen "& ...

  7. 使用sklearn机器学习库实现线性回归

    import numpy as np  # 导入科学技术框架import matplotlib.pyplot as plt  # 导入画图工具from sklearn.linear_model imp ...

  8. SVN——Couldn't perform atomic initialization

    前言   今天早上刚刚打开我的电脑,就被李总他们告知不能正确用SVN从服务器上下载代码了,然后看问题吧.问题其实也是本文的标题,并不难解决,写下来留个记录,顺便也算是一种分享.问题截图如下:    造 ...

  9. fedora 28 安装 wine 运行 uTorrent 解决linux 端,pt 资源下载问题

    fedora 28 仓库中,资源比较多.使用 wine 运行windows 程序,可以一定程度上解决软件跨平台问题. 搜索: Last metadata expiration check: :: ag ...

  10. Linux运维平台工具:pstree、ps、top、htop、free、vmstat、dstat、kill、killall

    1.pstree命令 查看进程树,centos7下统一由systemd进行管理 [root@ELK-chaofeng systemd]# pstree systemd─┬─AliYunDun───*[ ...