算出所有节点所处的层数s,然后按P*(1+r%)^s计算即可
#include <iostream>#include <vector>#include<cmath>#PRagma warning(disable:4996)using namespace std;struct node { vector<int> son; int lev;//层数 node() { lev = 0; } int x;//个数};vector<node> all;int N;double P, r;void bfs(int index){ for (auto &x : all[index].son) { all[x].lev = all[index].lev + 1; bfs(x); }}int main() { cin >> N >> P >> r; r = r / 100 + 1; all.resize(N); for (int t = 0;t < N;t++)//存储输入 { int temp; cin >> temp; if (temp == 0) cin >> all[t].x; while (temp--) { int te; cin >> te; all[t].son.push_back(te); } } bfs(0);//计算各个节点的层数 double sum = 0; for (auto x : all) if (x.son.empty()) sum += P*pow(r, x.lev)*x.x; printf("%.1f/n", sum);}新闻热点
疑难解答