pat 1079 total sales of supply chain

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#define MAX 100010
using namespace std;
int N ;
double p , r;
struct node
{
int data;
int layer;
vector<int> child;
}Node[MAX];
void layerOrder(int root)
{
queue<int> q;
Node[root].layer = 0;
q.push(root);
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i = 0 ; i < Node[now].child.size(); ++i)
{
int child = Node[now].child[i];
Node[child].layer = Node[now].layer +1;
q.push(child);
}
}
}
double sum = 0;
void preOrder(int root)
{
if(Node[root].data != 0)
{
sum += p * pow(1+r , Node[root].layer) * Node[root].data;
return ;//此处还是加上return比较好!!!
}
for(int i= 0 ; i < Node[root].child.size() ; ++i)
{
preOrder(Node[root].child[i]);
}
}
void DFS(int index, int depth)
{
if(Node[index].child.size() == 0)
{
sum += p * 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()
{
scanf("%d%lf%lf" , &N , &p , &r);
r /= 100;
int i,j;
for(i = 0 ; i < N ; ++i)
{
int num = 0;
scanf("%d" , &num);
Node[i].data =0;
if(num == 0)
{
scanf("%d" , &Node[i].data);
continue;
}
int mem;
for(j = 0 ; j < num ; ++j)
{
scanf("%d" , &mem);
Node[i].child.push_back(mem);
}
}
int root = 0;
layerOrder(root);
preOrder(root);
printf("%.1f" , sum);
return 0;
}

热评文章