1106 Lowest Price in Supply Chain (25分) C++

2024-06-08 17:32
文章标签 c++ 25 chain supply 1106 lowest price

本文主要是介绍1106 Lowest Price in Supply Chain (25分) C++,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1106 Lowest Price in Supply Chain

  • 题目描述
  • 解题思路
  • 代码

题目描述

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 (≤10​5​​), 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:

K​i​​ ID[1] ID[2] … ID[K​i​​]

where in the i-th line, K​i​​ 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. K​j​​ 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 1 0 10 10^{10} 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

解题思路

每个节点的卖出价格都是父节点价格的 ( 1 + r % ) (1+r\%) (1+r%)倍,消费者只会从叶子节点买入,价格是 P ∗ ( 1 + r % ) h P*(1+r\%)^h P(1+r%)h, h h h是该叶子节点的高度,这里 P , r P,r P,r都是常量,所以只要找到最矮的叶子节点所在的高度,以及最矮叶子节点的个数(题目的另一个输出)
代码上只要在层序遍历的框架中套入寻找叶子节点的逻辑即可,层序遍历到的第一个叶子节点是最矮的,接着再记录该层叶子节点的数量

代码

#include <iostream>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
#define DEBUG
#ifdef DEBUG
#define cin ifile
ifstream ifile("input.txt");
#endifint main(){int N;double P,r;cin>>N>>P>>r;vector< vector<int> > tree(N);int ki = 0,id = 0;for(int i=0;i<N;++i){cin>>ki;for(int j=0;j<ki;++j){cin>>id;tree[i].push_back(id);}}queue<int> q;q.push(0);//cur: index of current node//n_sup: sum of current node's child//level: the height of the current node//remain: remaining nodes of this height//n_cand: the amount of lowest leaf nodeint cur = 0,n_sup = 0,level=0,remain=1,n_cand = 0;while(!q.empty()){cur = q.front();q.pop();remain--;n_sup = tree[cur].size();//find leaf nodeif(n_sup == 0){n_cand=1;int candiate = 0;//find other leaf node in the same heightfor(int i=0;i<remain;++i){candiate = q.front();q.pop();if(tree[candiate].empty()){n_cand++;}}break;}for(int i=0;i<n_sup;++i){q.push(tree[cur][i]);}if(remain==0){level++;remain = q.size();}}printf("%.04f %d",P*pow(1+r/100,level),n_cand);return 0;
}

这篇关于1106 Lowest Price in Supply Chain (25分) C++的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1042836

相关文章

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat