PAT 1079 Total Sales of Supply Chain(树遍历+BFS)
来源:互联网 发布:数据库系统的二级映射 编辑:程序博客网 时间:2024/06/05 16:46
题目
https://www.patest.cn/contests/pat-a-practise/1079
题意:将零售商-经销商-供应商这条链抽象为一棵树;供应商作为根节点,出货物的价格为p,然后从根结点开始每往下走一层,该层的货物价格将会在父亲结点的价格上增加r%,给出每个叶结点(即零售商)的货物量,求他们的价格之和。
解题思路
供应商-经销商-零售商三者的关系可以看作是树的根节点-内部节点-叶子结点的关系。因此问题就转化成求每个叶子结点的层号,然后乘以相应系数,累加即可。
一次BFS/DFS解决,DFS代码比较简洁。
AC代码
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <queue>#include <cmath>using namespace std;const int maxn = 100005;vector<int> follow[maxn]; //记录节点i的后续节点int level[maxn]; //节点的层次int visited[maxn] = {false};double sale[maxn] = {0}; //零售商的编号集合vector<int> retail; //零售商i的卖出数量void BFS(int s){ queue<int> q; q.push(s); visited[s] = true; level[s] = 0; //根节点层号为0 int out, son; while (!q.empty()) { out = q.front(); q.pop(); for (int i = 0; i < follow[out].size(); ++i) { son = follow[out][i]; if (!visited[son]) //保证子节点没访问过 { level[son] = level[out] + 1; //子节点层次为父节点层次+1 visited[son] = true; q.push(son); } } }}void DFS(int index, int depth){ if (follow[index].size() == 0) //到达叶子结点 { level[index] = depth; //也可以直接在这里累加到ans return; } for (int i = 0; i < follow[index].size(); ++i) //对每个子节点搜索 DFS(follow[index][i], depth+1);}int main(){ int n; double p, r; int cnt, tmp; cin >> n >> p >> r; for (int i = 0; i < n; ++i) //处理每个编号为i的节点 { cin >> cnt; if (cnt == 0) //叶子结点 { cin >> sale[i]; retail.push_back(i); //零售商的编号集合 continue; } for (int j = 0; j < cnt; ++j) { cin >> tmp; follow[i].push_back(tmp); } } BFS(0); //BFS //DFS(0, 0); //DFS double ans = 0; for (int i = 0; i < retail.size(); ++i) //累加零售商的售价 { tmp = retail[i]; ans += sale[tmp] * p * pow(1.0+r/100, level[tmp]); } printf("%.1f\n", ans); return 0;}
阅读全文
0 0
- PAT 1079 Total Sales of Supply Chain(树遍历+BFS)
- PAT-1079 Total Sales of Supply Chain (树的遍历)
- pat 1079 Total Sales of Supply Chain
- 1079. Total Sales of Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)
- PAT 1079. Total Sales of Supply Chain (25)(bfs,树,树的层数)
- 【PAT】1079. Total Sales of Supply Chain
- PAT--1079. Total Sales of Supply Chain
- Pat(Advanced Level)Practice--1079(Total Sales of Supply Chain)
- PAT 1079-Total Sales of Supply Chain (25)
- pat 1079. Total Sales of Supply Chain(树模拟)
- 1079 Total Sales of Supply Chain
- PAT程序设计考题——甲级1079( Total Sales of Supply Chain ) C++实现
- PAT (Advanced Level) 1079. Total Sales of Supply Chain (25) 总叠加售价 BFS
- PAT 1079. Total Sales of Supply Chain (25) BFS+快速幂
- PAT 1079. Total Sales of Supply Chain (25)
- PAT (Advanced) 1079. Total Sales of Supply Chain (25)
- PAT Advanced Level 1079. Total Sales of Supply Chain (25)
- PAT A 1079. Total Sales of Supply Chain (25)
- Javascript设计模式 -- 灵活的javascript
- pthread-win32库编译及使用方法注意事项
- 统计学习(六):重抽样方法
- redis配置密码验证
- CString类常用方法----Left(),Mid(),Right()……
- PAT 1079 Total Sales of Supply Chain(树遍历+BFS)
- PHP将Excel导入数据库及数据库数据导出至Excel的方法 亲测
- 单例模式 (二) 延迟加载/"懒汉模式" —— 使用DCL双检查锁机制
- Teamcenter11.2.3.1 安装时遇到的TreeCache无法创建的问题
- 微信支付签名遇到的坑
- Unity 脚本之间的消息传递,事件管理
- JavaScript(3)
- ios10.1上 交叉编译 boost1_64_0
- 第一个annotation版的HelloWorld