PAT(甲级)渡劫(十四)-Total Sales of Supply Chain(25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xr469786706/article/details/87631530

PAT(甲级)渡劫(十四)-Total Sales of Supply Chain(25)

题目大意:

计算最小的层数与该层数上的叶子节点数目即可

代码如下:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

double sum = 0.0,p,r;
int n;
int amount[101024];     // 存储商品的数量
vector<vector<int> > v; // 邻接链表

void dfs(int u,int dep){
    if(v[u].size() == 0){
        sum += amount[u]*p*pow((1+r/100),dep);
    }
    for(int i = 0 ; i < v[u].size() ; ++i){
        dfs(v[u][i],dep+1);
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d%lf%lf",&n,&p,&r);
    v.resize(n);
    for(int i = 0 ; i < n ; i++){
        int k;
        scanf("%d",&k);
        if(k > 0){
            int id;
            while(k--){
                scanf("%d",&id);
                v[i].push_back(id);
            }
        }else{
            scanf("%d",&amount[i]);
        }
    }
    dfs(0,0);
    printf("%.1lf\n",sum);
    return 0;
}

运行结果:

猜你喜欢

转载自blog.csdn.net/xr469786706/article/details/87631530
今日推荐