1106 Lowest Price in Supply Chain
1. 本题和1090 Highest Price in Supply Chain适成对比,都是先构建一棵树,但本题是求最小层数和个数,链接题是求最大层数和个数。在极值更换和个数更新方面,两道题是一样的,但要注意,如果是求最小,一开始要初始化成最大,求最大,一开始要初始化成最小。
2. 我一开始尝试用链接题的DFS修改来通过这道,发现行不通,原因是DFS只会让层数增大,不可能越增大反而更容易满足,于是改成层次遍历,结点也加上层的属性,就做出来了。
AC代码
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath>
typedef long long LL;using namespace std;const int maxn = 100010;int n;//结点总数
double initPrice,rate;//出厂价和利率
int endDepth = maxn,deepNum= 0;struct Node{vector<int> children;int layer;
}node[maxn]; bool appear[maxn] = {false};int findRoot(){for(int i=0;i<n;i++){if(!appear[i])return i;}
} void layerOrder(int root){queue<int> que;node[root].layer = 0;que.push(root);while(!que.empty()){int top = que.front();que.pop();int size = node[top].children.size();if(!size){//到了叶子结点了,看能不能替换最小层数 if(node[top].layer<endDepth){endDepth = node[top].layer;deepNum = 1;}else if(node[top].layer==endDepth)deepNum++;}else{for(int i=0;i<size;i++){int childI = node[top].children[i];node[childI].layer = node[top].layer+1;que.push(childI);}}}
}int main(){scanf("%d %lf %lf",&n,&initPrice,&rate); int root,childNum,nodeIdx;for(int i=0;i<n;i++){scanf("%d",&childNum);while(childNum--){scanf("%d",&nodeIdx);appear[nodeIdx] = true;node[i].children.push_back(nodeIdx);}}root = findRoot();layerOrder(root);double hPrice = initPrice*pow(1+rate/100,endDepth);printf("%.4f %d",hPrice,deepNum); return 0;
}
1106 Lowest Price in Supply Chain相关推荐
- PAT甲级1106 Lowest Price in Supply Chain:[C++题解]树、结点到根结点的距离、树形dp、记忆化搜索
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这道题是第三次做了. 和PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树.结点到根结点的 ...
- 1106 Lowest Price in Supply Chain(甲级)
1106 Lowest Price in Supply Chain (25分) A supply chain is a network of retailers(零售商), distributors( ...
- PAT甲级 -- 1106 Lowest Price in Supply Chain (25 分)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 1106 Lowest Price in Supply Chain (25)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)
统计树的最小层数以及位于该层数上的叶子节点个数即可. 代码里建树我用了邻接链表的存储方式--链式前向星,不了解的可以参考,非常好用: http://www.cnblogs.com/chenxiwenr ...
- PAT_A1106#Lowest Price in Supply Chain
Source: PAT A1106 Lowest Price in Supply Chain (25 分) Description: A supply chain is a network of re ...
- 【PAT】A1106 Lowest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- pat1106 Lowest Price in Supply Chain
题意:给出一个供应链,一个根,·每经过一个节点售价增加百分之r,求最少要花多少钱从零售商得到商品,并求出有多少个零售商提供最低的价格. 思路:bfs. 代码 #include <iostream ...
最新文章
- ASP.NetViewState的实现方案
- 2021牛客多校7 - xay loves monotonicity(线段树区间合并)
- php 工资 2018,佛山市2018年平均工资(社平工资)
- zemax光学设计超级学习手册_穿越十年的一个ZEMAX光学设计案例
- TCMalloc内存分配器如何减少内存碎片?
- CentOS7——安装JDK和Tomcat
- 绿色五月我们能做点什么
- linux操作系统常用命令
- jsoup爬虫实战详解之新浪
- VMware Horizon View许可证如何工作?
- 课时1 Excel简介与基本操作
- 手机市场的竞争,用户价值才是硬道理
- 读研究生时,还有【暑假】么?
- 半次元收藏夹爬虫(残疾版,不喜勿喷)
- Blazor预研与实战
- 【阅读论文】博-自动化眼底图像分析技术可筛查糖尿病患者的视网膜疾病--第三章--QA
- python中global什么意思_python中的global关键字的使用方法
- 虎牙测试工程师校招面经
- 推荐一款网络收音机,学英文很棒哟
- Unity 环境变量设置