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

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int MAX_N = 100000;
int N;
double P, r, sum = 0.0;

struct Node
{
	vector<int> child;
	int level, sale;
}node[MAX_N];
queue<Node> Q;

int main()
{
	cin >> N >> P >> r;
	r = 1 + r * 0.01;
	int K, id;
	for (int i = 0; i < N; i++)
	{
		cin >> K;
		if (K > 0)
		{
			for (int j = 0; j < K; j++)
			{
				cin >> id;
				node[i].child.push_back(id);
			}
		}
		else
			cin >> node[i].sale;
	}
	Q.push(node[0]);
	while (!Q.empty())
	{
		Node tmp = Q.front();
		Q.pop();
		for (int i = 0; i < tmp.child.size(); i++)
		{
			node[tmp.child[i]].level = tmp.level + 1;
			Q.push(node[tmp.child[i]]);
		}
	}
	for (int i = 0; i < N; i++)
	if (node[i].sale > 0)
		sum += pow(r, node[i].level) * node[i].sale;
	printf("%.1f\n", sum * P);
}

你可能感兴趣的:(PAT (Advanced) 1079. Total Sales of Supply Chain (25))