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

思路:

1. 建立结构体,price记录价格,childNum记录孩子个数,child记录孩子标号

2. 全局变量minPrice记录最小价格,cnt记录最小价格的个数

2. 进行输入

3. 对树进行dfs,先计算当前的价格,判断孩子个数为0,若价格低于最小价格,进行更新,cnt = 1 !!!

若价格相等,则只更新cnt!

#include <iostream>
#include <vector>
using namespace std;
struct node{double price;int childNum;vector<int> child;
};
vector<node> supply;
int N;
double r; //N个chain上的人,r%的比率
double P; //P为最初的价格
double minPrice = 100000000000;
int cnt = 0;
void dfs(int root, double p){supply[root].price = p;if(supply[root].childNum == 0){  //到达叶子结点if(minPrice > p){cnt = 1;minPrice = p;}else if(minPrice == p){cnt++;}}for (int i = 0; i < supply[root].child.size(); i++){dfs(supply[root].child[i], p * (100+r)/100.0);}
}
int main(){scanf("%d %lf %lf", &N, &P, &r);supply.resize(N);for (int i = 0; i < N; i++){int k;scanf("%d", &k);if(k == 0) {supply[i].childNum = k;}else{supply[i].childNum = k;supply[i].child.resize(k);}for(int j = 0; j < k; j++){scanf("%d", &supply[i].child[j]);}}dfs(0,P);printf("%.4f %d", minPrice, cnt);return 0;
}

柳神代码:

等我吃过饭回来学习~~

补思路:

1. 没有用结构体,而是直接用了vector数组,v[i]表示i的孩子

2. dfs时有一个提前返回的判断,若当前的深度比最小深度大,直接返回

3.dfs统计的是最小深度,直接在最后进行计算。

#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;vector<int> v[100005];  //v[i]表示第i个的孩子结点int mindepth = 99999999, minnum = 1;void dfs(int index, int depth) {if(mindepth < depth)return ;if(v[index].size() == 0) {  //没有孩子if(mindepth == depth)minnum++;else if(mindepth > depth) {mindepth = depth;minnum = 1;}}for(int i = 0; i < v[index].size(); i++)dfs(v[index][i], depth + 1);
}int main() {int n, k, c;double p, r;scanf("%d %lf %lf", &n, &p, &r);for(int i = 0; i < n; i++) {scanf("%d", &k);for(int j = 0; j < k; j++) {scanf("%d", &c);v[i].push_back(c);}}dfs(0, 0);printf("%.4f %d", p * pow(1 + r/100, mindepth), minnum);return 0;
}

PAT甲级 -- 1106 Lowest Price in Supply Chain (25 分)相关推荐

  1. PAT甲级1106 Lowest Price in Supply Chain:[C++题解]树、结点到根结点的距离、树形dp、记忆化搜索

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这道题是第三次做了. 和PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树.结点到根结点的 ...

  2. PAT甲级 -- 1090 Highest Price in Supply Chain (25 分)

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

  3. 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

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

  4. 1106 Lowest Price in Supply Chain (25)

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

  5. PAT甲级 -- 1079 Total Sales of Supply Chain (25 分)

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

  6. PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)

    统计树的最小层数以及位于该层数上的叶子节点个数即可. 代码里建树我用了邻接链表的存储方式--链式前向星,不了解的可以参考,非常好用: http://www.cnblogs.com/chenxiwenr ...

  7. 1106 Lowest Price in Supply Chain(甲级)

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

  8. PAT甲级1090 Highest Price in Supply Chain:[C++题解]树、结点到根结点的距离、记忆化搜索、树形dp

    文章目录 题目分析 题目链接 题目分析 来源:acwing 和PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树.结点到根结点的距离.树形dp.记忆化搜索是 ...

  9. 1106 Lowest Price in Supply Chain

    1. 本题和1090 Highest Price in Supply Chain适成对比,都是先构建一棵树,但本题是求最小层数和个数,链接题是求最大层数和个数.在极值更换和个数更新方面,两道题是一样的 ...

最新文章

  1. MST(最小生成树)的构造
  2. kafka grpc_模型服务:流处理与使用Java,gRPC,Apache Kafka,TensorFlow的RPC / REST
  3. meta标签的用处详解
  4. simpledateformat线程不安全_ArrayList为什么线程不安全?
  5. 轻博客:企业品牌互动传播利器
  6. pandas 数据处理进阶
  7. url主机域名可以省略_接口自动化测试(三):关于URL
  8. initializer element is not a compile-time constant
  9. 算法笔记_面试题_4.树的遍历(前序/中序/后续遍历)
  10. CPU纯软件半虚拟化技术
  11. fortan dll在本地可以运行成功,移植到其他电脑上报错Exception in thread main java.lang.UnsatisfiedLinkError: 找不到指定的模块。
  12. 做电子相册思路 c语言,C/C++编程笔记:C语言制作情侣必备《爱情电子相册》,源码解析!...
  13. 比 Java 更强大的 kotlin.Deprecated
  14. 太阳的光和灯光有什么区别_太阳光和灯光有什么区别
  15. php依赖注入 简书,PHP 依赖注入容器
  16. PAT甲级 A1030
  17. 将本地镜像发布到阿里云仓库
  18. win10性能选项让计算机,一招开启win10“最强”模式,让你的电脑性能急速飙升!...
  19. c语言学籍管理系统设计,c语言学籍信息管理系统设计
  20. BetaFlight模块设计之十七:pinioBox任务分析

热门文章

  1. Ubuntu LXC
  2. eBPF在大厂的应用
  3. Spring循环依赖的三种方式,你都清楚吗?
  4. 深度解密Go语言之pprof
  5. Linux线程(六)
  6. 区分多种类型的输入输出
  7. 为什么视频压缩如此重要
  8. 广播IP转型报告:2021年广播公司面临的最大挑战
  9. Hulu直播服务难点解析(二):系统设计与实现
  10. SRS流媒体服务器——单机环境搭建和源码目录介绍