算出所有節點所處的層數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);}新聞熱點
疑難解答