1079 Total Sales of Supply Chain(树的遍历,dfs)

1079 Total Sales of Supply Chain (25 分)

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the total sales from all the retailers.

Input Specification:

Each input file contains one test case. For each case, the first line contains three positive numbers: N (≤10​5​​ ), the total number of the members in the supply chain (and hence their ID's are numbered from 0 to N−1, and the root supplier's ID is 0); P, the unit price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:
Ki ID[1] ID[2] ... ID[Ki]

where in the i-th line, K​i​​ is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID's of these distributors or retailers. Kj​​ being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after Kj​​ . All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will not exceed 1010​​ .

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3

Sample Output:

42.4

题目大意

供应链中有供应商、经销商、零售商,且不存在环形供应链。输入文件中提及当节点是零销商时,输入零销商的货物量,否则,输入这个节点下面的供应商/零销商ID。

分析

本题考查树的遍历,此处遍历采取的是dfs,当访问叶子节点时,计算货物总价。

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
vector<bool> isexist;
vector<vector<int> > v;
double ans=0.0;
int n;
double p,r;
map<int,int> productNum;
void dfs(int u,int depth){
    if(v[u].size()==0){
        ans+=productNum[u]*p*pow(1+r/100.0,depth);
    }else{
        for(int w=0;w<v[u].size();w++){
            dfs(v[u][w],depth+1);
        }
    }
}
int main(){
    cin>>n>>p>>r;
    isexist.resize(n);
    v.resize(n);
    fill(isexist.begin(),isexist.end(),false);
    for(int i=0;i<n;i++){
        int cnt,val;
        cin>>cnt;
        if(cnt==0) {
            cin>>val;
            isexist[val]=true;
            productNum[i]=val;
        }
        else
            for(int j=0;j<cnt;j++) {
                cin>>val;
                isexist[val]=true;
                v[i].push_back(val);
            }
    }
    int root=0;
    while(isexist[root]==true) root++;
    dfs(root,0);
    printf("%.1lf\n",ans );
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,163评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,482评论 0 23
  • 需求说明 需要将markdown格式的文件生成docx,比较特殊的是需要将无序列表和有序列表转化为大写字母的有序列...
    markdown阅读 7,087评论 0 5
  • 简单用法:绑定一些函数,用于传参、调用 扩充作用域:对象和方法不需要有任何的耦合关系 call方法的简单模拟实现
    一世长安乱阅读 903评论 0 0
  • 一见钟情是个伪命题,听起来像某种廉价荷尔蒙爆发后,为了将其包裹成众人所认可的事物而扣上的名号。 “此间少年”、“彼...
    指尖上的独角兽阅读 2,738评论 0 0