1079 Total Sales of Supply Chain

Posted CSU迦叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1079 Total Sales of Supply Chain相关的知识,希望对你有一定的参考价值。

1. 这道题考察的是树的层次遍历,结点需要有层这个属性,对于我来说,难点在于什么时候给层赋值,看书后知道应该是在加入队列之前(不管是根节点还是之后所有节点)。

2. 一开始算得216,原因是把r直接加一当作底数,其实中间商没有那么黑。。。应该是除以100再加1当作底数。

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;//出厂价和利率 
 
struct Node{
	int layer = 0;
	double price;
	vector<int> children;
	int amount;
}node[maxn];

bool appear[maxn] = {false};//用于寻找根节点

int findRoot(){
	for(int i=0;i<n;i++){
		if(!appear[i])return i;
	}
}

double layerOrder(int root){
	double sum = 0;
	queue<int> que;
	que.push(root);
	while(!que.empty()){
		int top = que.front();
		que.pop();
		int childN =  node[top].children.size();//当前结点的叶子结点数量 
		if(childN){//说明不是叶子结点 
			for(int i=0;i<childN;i++){
				int childI = node[top].children[i];
				node[childI].layer = node[top].layer+1;
				que.push(childI);
			}
		}else{//是叶子结点 
			sum += initPrice*node[top].amount*pow(rate/100+1,node[top].layer);
		}
	}
	
	return sum;
} 


int main(){
	
	scanf("%d %lf %lf",&n,&initPrice,&rate);	
	int childrenN,childIdx,amount;
	for(int i=0;i<n;i++){
		scanf("%d",&childrenN);
		if(childrenN){//说明不是叶子结点 
			while(childrenN--){
				scanf("%d",&childIdx);
				appear[childIdx] = true;
				node[i].children.push_back(childIdx);
			} 
		}else{
			scanf("%d",&amount);
			node[i].amount = amount; 
		}
	}
	
	int root = findRoot();
	
	double ans = layerOrder(root);
	
	printf("%.1f",ans); 
	 
	return 0;
}

以上是关于1079 Total Sales of Supply Chain的主要内容,如果未能解决你的问题,请参考以下文章

1079. Total Sales of Supply Chain (25)

PAT 1079 Total Sales of Supply Chain[比较]

1079 total sales of supply chain

1079 Total Sales of Supply Chain

PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)

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