您的位置:首页 > 大数据 > 人工智能

PAT 1106 Lowest Price in Supply Chain

2017-03-20 11:00 330 查看

1106. Lowest Price in Supply Chain (25)

时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA 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 chainhas 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 theroot 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 being0 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 theall the prices will not exceed 1010.Sample Input:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
0
0
0
Sample 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");}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: