1079. Total Sales of Supply Chain (25)

来源:互联网 发布:淘宝中国造 编辑:程序博客网 时间:2024/06/05 06:52

算出所有节点所处的层数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);}
0 0
原创粉丝点击