PAT 1106 Lowest Price in Supply Chain
来源:互联网 发布:电子工程师软件 编辑:程序博客网 时间:2024/06/06 02:18
1106. Lowest Price in 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 lowest price a customer can expect from some 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 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. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 1010.
Sample Input:10 1.80 1.003 2 3 51 91 41 702 6 11 8000Sample Output:
1.8362 2
题目:求一棵树叶节点中层数最低的层数,和有几个最低层数叶节点
思路:
建树,给每个节点赋层数值,遍历所有叶节点,找层数最低的
启示:
给树的每一个节点赋层数值,最好单独用层次遍历一个个赋,一边建树一边赋值不靠谱
Code:
#include <iostream>#include <string>#include <vector>#include <algorithm>#include <map>#include <queue>#include <stdio.h>#include <math.h>#define MAXN 100002struct TreeNode { int value; TreeNode* first_child; TreeNode* next_sibling; TreeNode* parent; int level; bool is_leaves;};int main() { int num_nodes; double root_price, increase_rate; TreeNode* nodes[MAXN]; std::vector<TreeNode*> leaves; std::vector<TreeNode*> unleveled_nodes; scanf("%d %lf %lf", &num_nodes, &root_price, &increase_rate); for (int i = 0; i < num_nodes; i++) { nodes[i] = (TreeNode*)malloc(sizeof(TreeNode)); nodes[i]->parent = NULL; nodes[i]->value = i; nodes[i]->first_child = NULL; nodes[i]->next_sibling = NULL; nodes[i]->is_leaves = false; nodes[i]->level = -1; if (i == 0) { nodes[i]->level = 0; } } for (int i = 0; i < num_nodes; i++) { int k; std::cin >> k; if (k > 0) { int fc; std::cin >> fc; nodes[i]->first_child = nodes[fc]; nodes[fc]->parent = nodes[i]; nodes[fc]->level = nodes[i]->level + 1; TreeNode* p = nodes[fc]; for (int j = 0; j < k - 1; j++) { int sb; std::cin >> sb; p->next_sibling = nodes[sb]; nodes[sb]->parent = nodes[i]; p = p->next_sibling; if (nodes[i]->level != -1) { p->level = nodes[i]->level + 1; } else { unleveled_nodes.push_back(nodes[i]); unleveled_nodes.push_back(p); } } } else { nodes[i]->is_leaves = true; leaves.push_back(nodes[i]); } } std::queue<TreeNode*> q; q.push(nodes[0]); while (!q.empty()) { TreeNode* t = q.front(); q.pop(); TreeNode* f = t->first_child; while (f) { q.push(f); f->level = t->level + 1; f = f->next_sibling; } } int count = 0; int min_level = MAXN; for (int i = 0; i < leaves.size(); i++) { TreeNode* node = leaves[i]; if (min_level > node->level) { count = 1; min_level = node->level; } else if (min_level == node->level) { count++; } } // printf("min_level is %d\n", min_level); printf("%.4f %d", root_price * pow((1+increase_rate/100), min_level), count); system("pause");}
- PAT 1106 Lowest Price in Supply Chain
- PAT 1106 Lowest Price in Supply Chain
- PAT--1106. Lowest Price in Supply Chain
- 【PAT】1106. Lowest Price in Supply Chain
- 1106 Lowest Price in Supply Chain
- 【PAT】1106. Lowest Price in Supply Chain (25)
- PAT 1106. Lowest Price in Supply Chain (25)
- pat 1106. Lowest Price in Supply Chain (25)
- PAT 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25) PAT甲级
- pat 1106. Lowest Price in Supply Chain (25)
- PAT A1106. Lowest Price in Supply Chain (25)
- pat-a1106. Lowest Price in Supply Chain (25)
- PAT A 1106. Lowest Price in Supply Chain (25)
- 【PAT】【Advanced Level】1106. Lowest Price in Supply Chain (25)
- Pat(A) 1106. Lowest Price in Supply Chain (25)
- PAT 甲级 1106. Lowest Price in Supply Chain (25)
- PAT (Advanced Level) Practise 1106 Lowest Price in Supply Chain (25)
- python: sum 函数 --sum(axis=1)
- 通信信号中 复信号处理
- 前端性能优化总结(三)
- iOS开发多线程篇--概述
- Java通过Spring配置发送QQ邮件
- PAT 1106 Lowest Price in Supply Chain
- 一次完整的HTTP请求所经历的7个步骤
- 网络下载图片 多线程 -复盘
- Linux/Unix ulimit命令详解
- 回归模型的分析一
- Linux下的tar压缩解压缩命令详解
- 分布式模式之Broker模式
- 解救空间不足无法启动的白苹果iPhone5S
- php exec java -jar 不能执行命令