1106 Lowest Price in Supply Chain
Posted CSU迦叶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章
1106 Lowest Price in Supply Chain (25 分)dfs
[建树(非二叉树)] 1106. Lowest Price in Supply Chain (25)
PAT甲级——A1106 Lowest Price in Supply Chain
PAT 甲级 1106 Lowest Price in Supply Chain (25分) (bfs)
1106 Lowest Price in Supply Chain
1106. Lowest Price in Supply Chain (25)树+深搜——PAT (Advanced Level) Practise