A1079 Total Sales of Supply Chain [dfs]

在这里插入图片描述

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = 100001;
struct node
{
	double data;
	vector<int>child;
}Node[maxn];
int n;
double p, r, ans = 0;
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);
	}
}
int main()
{
	int k, child;
	cin >> n >> p >> r;
	r /= 100;
	for (int i = 0; i < n; i++)
	{
		cin >> k;
		if (k == 0)
			cin >> Node[i].data;
		else
			for (int j = 0; j < k; j++)
			{
				cin >> child;
				Node[i].child.push_back(child);
			}
	}
	dfs(0, 0);
	printf("%.1f
", p * ans);
	return 0;
}
原文地址:https://www.cnblogs.com/Hsiung123/p/13812012.html