PAT1079 Total Sales of Supply Chain (25)

题目链接:

http://www.nowcoder.com/pat/5/problem/4309

题目描述:

题目要求根据某商品的供应链条,求零售商的销售额之和,零售商的售价不同在于每一级都有涨幅,对不同层的零售商按照所在层求销售额,然后全部加和。
只有叶子结点才是零售商。

题目分析:

记录树的层数的BFS的变形。

代码:

#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
double result,P,r;
int N;
int level[100005];
vector<vector<int>> map;
void bfs(){
    result=0;
    queue<int> q;
    q.push(0);
    while (!q.empty()){
        int i = q.front();
        q.pop();
        int lev = level[i];
        int size = map[i].size();
        for (int j = 0; j < size; j++){
            if (map[i][0] == 0){
                result += map[i][1] * P*pow(1+r*(1.0)/100,level[i]);
                break;
            }
            level[map[i][j]] = lev + 1;
            q.push(map[i][j]);
        }
    }
}
int main()
{
    memset(level, 0, sizeof(level));
    scanf("%d%lf%lf",&N,&P,&r);
    for (int i = 0; i < N; i++){
        vector<int> v;
        map.push_back(v);
        int nexts,next;
        cin >> nexts;
        if (nexts == 0){
            map[i].push_back(0);
            int retailers;
            cin >> retailers;
            map[i].push_back(retailers);
        }
        while (nexts--){
            cin >> next;
            map[i].push_back(next);
        }
    }
    bfs();
    printf("%.1lf",result);
    return 0;
}

你可能感兴趣的:(树,pat,bfs)