forked from richenyunqi/CCF-CSP-and-PAT-solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1079. Total Sales of Supply Chain.cpp
80 lines (80 loc) · 1.8 KB
/
1079. Total Sales of Supply Chain.cpp
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
//深度优先遍历
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
const gg MAX = 1e5 + 5;
vector<vector<gg>> tree(MAX);
vector<double> amount(MAX), price(MAX);
gg ni, ki, ai;
double ri;
void dfs(gg root) {
for (gg i : tree[root]) {
price[i] = price[root] * (1 + ri);
dfs(i);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> ni >> price[0] >> ri;
ri /= 100;
for (gg i = 0; i < ni; ++i) {
cin >> ki;
if (ki == 0) {
cin >> amount[i];
} else {
while (ki--) {
cin >> ai;
tree[i].push_back(ai);
}
}
}
dfs(0);
cout << fixed << setprecision(1)
<< inner_product(price.begin(), price.begin() + ni, amount.begin(),
0.0);
return 0;
}
//广度优先遍历
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
const gg MAX = 1e5 + 5;
vector<vector<gg>> tree(MAX);
vector<double> amount(MAX), price(MAX);
gg ni, ki, ai;
double ri;
void bfs(gg root) {
queue<gg> q;
q.push(root);
while (not q.empty()) {
auto t = q.front();
q.pop();
for (auto i : tree[t]) {
q.push(i);
price[i] = price[t] * (1 + ri);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> ni >> price[0] >> ri;
ri /= 100;
for (gg i = 0; i < ni; ++i) {
cin >> ki;
if (ki == 0) {
cin >> amount[i];
} else {
while (ki--) {
cin >> ai;
tree[i].push_back(ai);
}
}
}
bfs(0);
cout << fixed << setprecision(1)
<< inner_product(price.begin(), price.begin() + ni, amount.begin(),
0.0);
return 0;
}