PAT甲级题目1079 Total Sales of Supply Chain

题目大意:
供应商只有一个,经销商进货通过供应商,零售商进货通过经销商或供应商,每个商家都用0~N-1编号标出,供应商是1,现在需要求出从所有零售商手里买到商品需要的钱。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=100010;
int maxlevel=0,num=0;
double ans=0; 
struct node
{
	double weight;
	vector<int> child;
}Tree[maxn];
double price,rate;
void DFS(int index,int level);
int main()
{
	int n;
	int cnt;
	cin>>n>>price>>rate;
	rate/=100;
	for(int i=0;i<n;i++)
	{
		cin>>cnt;
		if(cnt==0)
		{
			int weight;
			cin>>weight;
			Tree[i].weight=weight;
		}
		else
		{
			for(int j=0;j<cnt;j++)
			{
				int temp;
				cin>>temp;
				Tree[i].child.push_back(temp);
			}
		}
	}
	DFS(0,0);
	printf("%.1f",price*ans);
	return 0;
}
void DFS(int index,int level)
{
	if(Tree[index].child.size()==0)//找到叶节点 
	{
		ans+=Tree[index].weight*pow(1+rate,level);//计算出货物总量,用重量增加代替价格增加 
		return;
	}
	for(int i=0;i<Tree[index].child.size();i++)//递归的访问每一个子节点 
		DFS(Tree[index].child[i],level+1);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值