1079 Total Sales of Supply Chain详解在代码中

本文介绍了一个用于计算供应链中产品成本的算法,通过深度优先搜索遍历树状结构的供应链网络,考虑每一层级的成本增加率,最终计算出产品的总销售价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

详解见代码

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <cstring>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <ctype.h>
using namespace std;
const int maxn = 100010;
struct BTnode {
	double data;
	vector<int> child;
}node[maxn];
double p, r, ans = 0;
int n;
void DFS(int index, int depth) {
	if (node[index].child.size() == 0) {
		ans += node[index].data * pow(1 + r, depth);
		return;
	}
	for (int i = 0; i < node[index].child.size(); i++) {
		DFS(node[index].child[i], depth + 1);//从根开始,每访问一个节点的孩子 depth+1
	}
}
int main() {
	scanf("%d%lf%lf", &n, &p, &r);
	r /= 100;
	int k, child;
	for (int i = 0; i < n; i++) {
		scanf("%d", &k);
		if (k == 0)
			scanf("%lf", &node[i].data);
		else {
			for (int j = 0; j < k; j++) {
				scanf("%d", &child);
				node[i].child.push_back(child);
			}
		}
	}
	DFS(0, 0);
	printf("%.1f", p * ans);//叶节点的data是指以p价格在根进的货量
	//而供应链每加一层,就乘(1+r),depth从0开始,到根,每往深度一步,就乘(1+r)
	//恰好depth是叶子的深度-1, 到了某叶子时,单价已经变成了pow(1+r,depth)
	//最后再*p即某叶子的销售额
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值