题目

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:

Ki ID[1] ID[2] … ID[Ki]

where in the i-th line, Ki 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. Kj 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 1010.

Sample Input:

10 1.80 1.00

3 2 3 5

1 9

1 4

1 7

0

2 6 1

1 8

000

Sample Output:

1.8362 2

题目分析

已知所有非叶子节点的所有子节点,求最短路径(根节点到叶子节点)的层数和该层叶子节点数

解题思路

思路 01

  1. dfs遍历树,参数h记录当前节点所在层数,统计所有层叶子结点数
  2. 找出出现叶子节点的最小层,并计算该层价格

思路 02

  1. dfs遍历树,参数p记录当前节点所在层的价格,min_h记录出现叶子节点的最小层,min_num记录max_p层叶子节点数

思路 03

  1. bfs遍历树,int h[n]数组记录当前节点所在层,统计所有层叶子结点数
  2. 找出出现叶子节点的最小层,计算该层价格

思路 04

  1. bfs遍历树,double ps[n]数组记录当前节点所在层的价格,min_h记录出现叶子节点的最小层,min_num记录max_p层叶子节点数

Code

Code 01(dfs)

  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5. const int maxn = 100000;
  6. vector<int> nds[maxn];
  7. double p,r;
  8. int cns[maxn],leaf[maxn],max_h;
  9. void dfs(int index, int h){
  10. max_h=max(max_h,h);
  11. if(cns[index]==0){
  12. leaf[h]++; //对应层级叶子节点数+1;
  13. return;
  14. }
  15. for(int i=0;i<cns[index];i++){
  16. dfs(nds[index][i],h+1);
  17. }
  18. }
  19. int main(int argc, char * argv[]) {
  20. int n,cid;
  21. scanf("%d %lf %lf",&n,&p,&r);
  22. for(int i=0; i<n; i++) {
  23. scanf("%d",&cns[i]);
  24. for(int j=0;j<cns[i];j++){
  25. scanf("%d",&cid);
  26. nds[i].push_back(cid);
  27. }
  28. }
  29. dfs(0,0);
  30. int min_h=0;
  31. while(min_h<=max_h&&leaf[min_h]==0)min_h++;
  32. printf("%.4f %d",p*pow(1+r*0.01,min_h),leaf[min_h]);
  33. return 0;
  34. }

Code 02(dfs 优化版)

  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5. const int maxn = 100000;
  6. vector<int> nds[maxn];
  7. double p,r;
  8. int cns[maxn],min_num,min_h=99999999;
  9. void dfs(int index, int h){
  10. if(min_h<h){
  11. return;
  12. }
  13. if(cns[index]==0){
  14. if(min_h==h){
  15. min_num++;
  16. }else if(min_h>h){
  17. min_h=h;
  18. min_num=1;
  19. }
  20. }
  21. for(int i=0;i<cns[index];i++){
  22. dfs(nds[index][i],h+1);
  23. }
  24. }
  25. int main(int argc, char * argv[]) {
  26. int n,cid;
  27. scanf("%d %lf %lf",&n,&p,&r);
  28. for(int i=0; i<n; i++) {
  29. scanf("%d",&cns[i]);
  30. for(int j=0;j<cns[i];j++){
  31. scanf("%d",&cid);
  32. nds[i].push_back(cid);
  33. }
  34. }
  35. dfs(0,0);
  36. printf("%.4f %d",p*pow(1+r*0.01,min_h),min_num);
  37. return 0;
  38. }

Code 03(bfs)

  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cmath>
  5. using namespace std;
  6. const int maxn=100000;
  7. vector<int> nds[maxn];
  8. double p,r;
  9. int cns[maxn],cnt,max_h,h[maxn],leaf[maxn];
  10. bool flag;
  11. void bfs() {
  12. queue<int> q;
  13. q.push(0);
  14. while(!q.empty()) {
  15. int now = q.front();
  16. q.pop();
  17. max_h=max(max_h,h[now]);
  18. if(cns[now]==0) {
  19. leaf[h[now]]++; //对应层级叶子结点数+1;
  20. } else {
  21. for(int i=0; i<cns[now]; i++) {
  22. h[nds[now][i]] = h[now]+1;
  23. q.push(nds[now][i]);
  24. }
  25. }
  26. }
  27. }
  28. int main(int argc,char * argv[]) {
  29. int n,k,cid;
  30. scanf("%d %lf %lf",&n,&p,&r);
  31. for(int i=0; i<n; i++) {
  32. scanf("%d",&cns[i]);
  33. for(int j=0; j<cns[i]; j++) {
  34. scanf("%d",&cid);
  35. nds[i].push_back(cid);
  36. }
  37. }
  38. h[0]=0;
  39. bfs();
  40. int min_h=0;
  41. while(min_h<=max_h&&leaf[min_h]==0)min_h++;
  42. printf("%.4f %d",p*pow(1+r*0.01,min_h),leaf[min_h]);
  43. return 0;
  44. }

Code 04(bfs 优化版)

  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cmath>
  5. using namespace std;
  6. const int maxn=100000;
  7. vector<int> nds[maxn];
  8. double p,r;
  9. int cns[maxn],cnt,min_h=9999999,min_num,h[maxn];
  10. bool flag;
  11. void bfs() {
  12. queue<int> q;
  13. q.push(0);
  14. while(!q.empty()) {
  15. int now = q.front();
  16. q.pop();
  17. if(cns[now]==0) {
  18. if(min_h>h[now]){
  19. min_h=h[now];
  20. min_num=1;
  21. }else if(min_h==h[now]){
  22. min_num++;
  23. }
  24. } else {
  25. for(int i=0; i<cns[now]; i++) {
  26. h[nds[now][i]] = h[now]+1;
  27. q.push(nds[now][i]);
  28. }
  29. }
  30. }
  31. }
  32. int main(int argc,char * argv[]) {
  33. int n,k,cid;
  34. scanf("%d %lf %lf",&n,&p,&r);
  35. for(int i=0; i<n; i++) {
  36. scanf("%d",&cns[i]);
  37. for(int j=0; j<cns[i]; j++) {
  38. scanf("%d",&cid);
  39. nds[i].push_back(cid);
  40. }
  41. }
  42. h[0]=0;
  43. bfs();
  44. printf("%.4f %d",p*pow(1+r*0.01,min_h),min_num);
  45. return 0;
  46. }

PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]的更多相关文章

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

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

  2. PAT甲级——1106 Lowest Price in Supply Chain(BFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90444872 1106 Lowest Price in Supp ...

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

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

  4. PAT 甲级 1106 Lowest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464 A supply chain is a ne ...

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

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

  6. PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]

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

  7. PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]

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

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

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

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

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

随机推荐

  1. NO2 pwd-touch-vim-vi-echo-重定向等命令

    ·查看网卡配置:cat/etc/sysconfig/network-scripts/ifcfg-eth0·改onboot=no:sed -i's#noboot=yes#g' /etc/sysconfi ...

  2. Spring Aop 原理分析

    @EnableAspectJAutoProxy Aop功能开启注解 为容器中导入 @Import(AspectJAutoProxyRegistrar.class)组件,在其重写方法中为 ioc容器 注 ...

  3. Golang的运算符-比较运算符

    Golang的运算符-比较运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.比较运算符概述 比较运算符也称为关系运算符,比较运算符返回的类型为bool类型,常见的比较运算符 ...

  4. POJ 1852:Ants

    Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11754   Accepted: 5167 Description ...

  5. c++数据结构排序

    #include<stdio.h> #include<stdlib.h> #include<time.h> typedef int ElemType; typede ...

  6. mysql基本知识的总结

    Mysql基本sql知识 Navicat快捷方式: 选中当前行 在行尾:shift+home 在行首:shift+end 执行当前行:ctrl+shift+R 复制当前行:ctrl+D 显示所有数据库 ...

  7. cf749 D. Leaving Auction

    #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL long long #define N 200005 #de ...

  8. 二十二、JavaScript之在对象中写函数

    一.代码如下 二.效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...

  9. 二十一、JavaScript之访问对象属性

    一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...

  10. js原型链理解(4)-经典继承

    经典继承就是组合继承,就是组合构造函数和原型链的优点混合继承. 1.避免引用类型的属性初始化 2.避免相同方法的多次初始化 function Super(name){ this.ages = [100 ...