1090. Highest Price in Supply Chain (25)
2015-11-23 13:12
483 查看
1.这道题目实则是一道关于的题目,树的每一层代表一个级别的供应商,树的高度越大,价格越高
如题目例子:
构成的树为
红色部分(0和8,一共两个节点)的价格最高,为1.8*1.01*1.01*1.01=1.85
2.采用合适的数据结构存储节点,然后BFS进行层次遍历即可
![](http://img.blog.csdn.net/20151123140334678?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
AC代码:
如题目例子:
9 1.80 1.00 1 5 4 4 -1 4 5 3 6
构成的树为
红色部分(0和8,一共两个节点)的价格最高,为1.8*1.01*1.01*1.01=1.85
2.采用合适的数据结构存储节点,然后BFS进行层次遍历即可
AC代码:
//#include<string> //#include <iomanip> #include<vector> #include <algorithm> //#include<stack> #include<set> #include<queue> #include<map> //#include<unordered_set> #include<unordered_map> //#include <sstream> //#include "func.h" //#include <list> #include<stdio.h> #include<iostream> #include<string> #include<memory.h> #include<limits.h> using namespace std; struct Node { vector<int> list; Node() :list(0){}; }; int main(void) { int n; double rootPrice; double higherPercent; cin >> n >> rootPrice >> higherPercent; vector<Node> supplier(n); int root; for (int i = 0; i < n; i++) { int Si; scanf("%d", &Si); if (Si == -1) root = i;//-1为根供应商 else//i的上级供应商为Si supplier[Si].list.push_back(i); } //进行层次遍历 queue<int> q; int count1 = 1, count2 = 0; q.push(root); int level = 0; int lastLevelMember = 0; while (!q.empty()) { for (int i = 0; i < count1; i++) { int head = q.front(); q.pop(); for (int j = 0; j < supplier[head].list.size(); j++) { q.push(supplier[head].list[j]); count2++; } } level++; lastLevelMember = count1; count1 = count2; count2 = 0; } double highestPrice = rootPrice; for (int i = 0; i < level-1; i++) {//level会把root也算作1层 highestPrice *= (1 + higherPercent / 100.0); } printf("%.2lf %d\n", highestPrice, lastLevelMember); return 0; }
相关文章推荐
- apk反编译出错:Exception in thread "main" brut.androlib.AndrolibException: Could not decode ars c file
- codeforces 579A Raising Bacteria
- Could not reliably determine the server's fully qualified domain name
- type_traits.h
- stl_pair.h
- MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]
- Codeforces Round #185 (Div. 2) C. The Closest Pair 构造
- 多线程(银行存款)使用notify()和wait()线程通信实现交替存款
- 217. Contains Duplicate
- 什么是领域驱动设计(Domain Driven Design)?
- LeetCode OJ——Submission Details
- BZOJ4320: ShangHai2006 Homework
- [AlwaysOn Availability Groups]AlwaysOn等待类型
- Domain Name System (DNS)
- NYOJ 150 Train Problem I
- 快速激活JetBrains系列产品 可以激活WebStorm11
- [AlwaysOn Availability Groups]AG扩展事件
- SSH留言板 com.sun.jersey.api.container.ContainerException 错误
- Jade之Plain Text
- LeetCode 子 Climbing Stairs