1090. Highest Price in Supply Chain (25)

来源:互联网 发布:淘宝售后客服怎么样 编辑:程序博客网 时间:2024/05/16 09:39

1.这道题目实则是一道关于的题目,树的每一层代表一个级别的供应商,树的高度越大,价格越高

如题目例子:

9 1.80 1.001 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的上级供应商为Sisupplier[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;}


0 0