【树的遍历】PAT A1106 Lowest Price in Supply Chain (25分)

疑问

暂无

代码

//注意精度
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxn = 100010;

struct node{
	int depth;
	int num;
	vector<int> child;
}Node[maxn];

//数量
int c=0; 
//输入结点数
int n=0;
//原始价格
double p=0;
//利润
double r=0; 
//总价格
double sale=0; 
//最小深度
int minDepth=maxn;


void preOrder(int root,int depth){
	Node[root].depth = 1+depth;
	if(Node[root].child.size() == 0){
		if(Node[root].depth < minDepth){
			minDepth = Node[root].depth;
			c = 1; 
		}else if(Node[root].depth == minDepth){
			c++;
		}
		return;
	}else{
		for(int i=0;i<Node[root].child.size();i++){
			preOrder(Node[root].child[i],Node[root].depth);
		}
		return;
	}
}

int main(){
	scanf("%d %lf %lf",&n,&p,&r);

	r = r/100;
	
	for(int i=0;i<n;i++){
		int num;
		scanf("%d",&num);
		if(num != 0){
			for(int j=0;j<num;j++){
				int child;
				scanf("%d",&child);
				Node[i].child.push_back(child);	
			}			
		}
	}
	
	preOrder(0,-1);
	
	sale = p * pow((1+r),minDepth);
	
	printf("%.4f %d",sale,c);
	return 0;
} 

反思

  1. 相比之前的A1190,我在这里参考了《算法笔记》上使用全局变量num的方法来实时记录最小深度的数量;
  2. 这里依然可以使用最简单的树的表达形式:vector<int> Node[maxn]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值