题目如下:

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 (<=105), 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, Ki 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

题目要求根据某商品的供应链条,求零售商的销售额之和,零售商的售价不同在于每一级都有涨幅,这个问题的本质就是BFS,对不同层的零售商按照所在层求销售额,然后全部加和。

因为只有叶子结点才是零售商,因此不能额外统计非零售商的情况,我们不区分叶子结点,而是让所有非叶子结点的销售量为0,这样就可以统一问题,计数全部结点了。

本题目中的图是一棵树,因此不必用visited来记录结点访问情况,因为不会出现重复访问的情况。

要记录层,定义变量tail和last,tail代表下一层的尾巴,last代表本层的尾巴,首先让last和tail都初始化为源点,接着在邻接点遍历时一直用tail记录该邻接点,当last和当前出队结点v一致时,说明这一层结束,这时候last指向的恰是这一层最后一个元素的最后一个邻接点,也就是下一层的尾巴,因此这时候让last=tail,就可以按照同样的逻辑进行下一层。

在每次换层时注意对价格的处理,对每个出队结点计算销售额,加和即可。

代码如下:

#include <iostream>
#include <vector>
#include <stdio.h>
#include <queue> using namespace std; int main()
{
int N;
double rootPrice,percent;
cin >> N >> rootPrice >> percent;
vector<vector<int> > graph(N);
vector<int> nodeSales(N);
for(int i = 0; i < N; i++){
int cnt,num;
scanf("%d",&cnt);
if(cnt == 0){
scanf("%d",&num);
nodeSales[i] = num;
graph[i].clear();
continue;
}else{
nodeSales[i] = 0;
}
for(int j = 0; j < cnt; j++){
scanf("%d",&num);
graph[i].push_back(num);
}
}
queue<int> q;
q.push(0);
double sum = 0;
double factor = rootPrice;
int tail = 0;
int last = 0;
while(!q.empty()){ int v = q.front();
q.pop(); sum += nodeSales[v] * factor; for(int i = 0; i < graph[v].size(); i++){
int w = graph[v][i];
q.push(w);
tail = w;
} if(v == last){
factor *= (1 + percent / 100);
last = tail;
} } printf("%.1lf\n",sum); return 0;
}

1079. Total Sales of Supply Chain (25) -记录层的BFS改进的更多相关文章

  1. 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...

  2. PAT 甲级 1079 Total Sales of Supply Chain (25 分)(简单,不建树,bfs即可)

    1079 Total Sales of Supply Chain (25 分)   A supply chain is a network of retailers(零售商), distributor ...

  3. 1090. Highest Price in Supply Chain (25) -计层的BFS改进

    题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...

  4. 1079. Total Sales of Supply Chain (25)-求数的层次和叶子节点

    和下面是同类型的题目,只不过问的不一样罢了: 1090. Highest Price in Supply Chain (25)-dfs求层数 1106. Lowest Price in Supply ...

  5. PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...

  6. 1079. Total Sales of Supply Chain (25)

    时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  7. PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)

    树的遍历. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  8. 【PAT甲级】1079 Total Sales of Supply Chain (25 分)

    题意: 输入一个正整数N(<=1e5),表示共有N个结点,接着输入两个浮点数分别表示商品的进货价和每经过一层会增加的价格百分比.接着输入N行每行包括一个非负整数X,如果X为0则表明该结点为叶子结 ...

  9. pat1079. Total Sales of Supply Chain (25)

    1079. Total Sales of Supply Chain (25) 时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

随机推荐

  1. 伊布(ib)

    [问题描述]ib 被困在了一个美术馆里,她需要收集美术馆内的每种颜料才能获得逃出美术馆的钥匙美术馆由 n*m 的房间构成,每个房间里有一种颜料,解锁进入后就可以收集.有的房间不能解锁,如果解锁的话会直 ...

  2. UVALive4727:jump

    约瑟夫环变式 设f[i][j]表示处理i个人,按照处理顺序,倒数第j个人是谁 则有f[i][j]=(f[i-1][j]+k)%i #include<cstdio> #include< ...

  3. 【bzoj4444 scoi2015】国旗计划

    题目描述 A 国正在开展一项伟大的计划 —— 国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此,国土安全局已经挑选了 NN 名优秀的边防 ...

  4. hdu 5479(括号问题)

    题意:类似"()","(())","()()" 是匹配的, 而 "((", ")(", " ...

  5. 【CodeVs 6128 Lence的方块们】

    ·希望除了内部人员以外能有人通过这道题,因为这是大米饼第一次改编的题 ·我所见到的"本题原版"的题解也很少,搜索一下应该是: #include<stdio.h> #in ...

  6. [APIO2011]

    来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------ A.[Apio2011]方格染色 ...

  7. Django中数据查询(万能下换线,聚合,F,Q)

    数据查询中万能的下划线基本用法: __contains: 包含 __icontains: 包含(忽略大小写) __startswith: 以什么开头 __istartswith: 以什么开头(忽略大小 ...

  8. SVN与Git

    一:SVN是什么?SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到S ...

  9. [ SSH框架 ] Hibernate框架学习之二

    一.Hibernate持久化类的编写规范 1.什么是持久化类 Hibernate是持久层的ORM影射框架,专注于数据的持久化工作.所谓持久化,就是将内存中的数据永久存储到关系型数据库中.那么知道了什么 ...

  10. nginx的yum安装,基本参数使用,编译参数说明和Nginx基本配置,模块安装

    nginx的yum安装从nginx官网获取源 vim /etc/yum.repose.d/nginx.repo[nginx]name=nginx repobaseurl=http://nginx.or ...