1079 Total Sales of Supply Chain

1079 Total Sales of Supply Chain (25分)

pow()函数:求x的y次方的值。
叶子节点为零售商,data保存销售商品的数量,child向量保存子节点。
DFS深度搜索查找叶子节点,累加销售额。
最后再乘以价格p。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;

double res = 0.0, p, r;

struct node {
int data;
vector<int> child;
};

vector<node> v;

void DFS(int index, int depth) {
if (v[index].child.size() == 0) {
res += v[index].data * pow(1 + r, depth);
return;
}
else {
for (int i = 0; i < v[index].child.size(); i++) {
DFS(v[index].child[i], depth + 1);
}
}
}

int main() {
int n, k, c;
scanf("%d %lf %lf", &n, &p, &r);
r = r / 100;
v.resize(n);
for (int i = 0; i < n; i++) {
scanf("%d", &k);
if (k == 0) {
scanf("%d", &v[i].data);
}
else {
for (int j = 0; j < k; j++) {
scanf("%d", &c);
v[i].child.push_back(c);
}
}
}
DFS(0, 0);
printf("%.1f", res * p);

return 0;
}