1090 Highest 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. 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 highest price we 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 they are numbered from 0 to N-1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number S~i~ is the index of the supplier for the i-th member. S~root~ for the root supplier is defined to be -1. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 10^10^.

Sample Input:

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output:1.85 2

题目大意:给出一个数组a[i] = j; 表示i的供应商是j, 当a[i]=-1的时候,表示i是最顶端的供应商。求供应链的最长长度,以及处于最长长度供应链末端零售商的人数
对给出的数据做以下处理:建立一个二维vector向量,chain[i]表示i为供应商,chain[i][j]表示j的供应商是i。chain[i].size()=0 则表示i是零售商
            maxdepth记录供应链的最长长度,cnt记录处于最长供应链末端零售商的人数。
这一题其实就是用数组建立树,并遍历树的过程。从顶端供应商root开始遍历,直到遍历到零售商为止(即chain[i].size()=0); 遍历过程中,如果发现当前的depth比maxdepth大,则更新maxd,并更新cnt=1;
若遍历过程中发现当前depth=maxdepth则更新cnt的值;

注意点:
  计算最高价格的时候,要用double类型, 用float类型,会有一些测试点不能通过,应该是溢出导致
  在dfs()遍历树的时候,不能调用dfs(index, depth++), 应该用dfs(index, depth+1); 前者会改变同意层次循环中的depth值导致最终结果错误


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int cnt=0, maxdepth=0;
double ans=0.0;
vector<vector<int>> chain;
void dfs(int index, int depth){if(chain[index].size()==0){if(depth>maxdepth){maxdepth=depth; cnt=1;}else if(depth==maxdepth) cnt++;return;}for(int i=0; i<chain[index].size(); i++) dfs(chain[index][i], depth+1);
}
int main(){int n, i, root, temp;double p, r;cin>>n>>p>>r;chain.resize(n);for(i=0; i<n; i++){cin>>temp;if(temp==-1){root=i; continue;}chain[temp].push_back(i);}dfs(root, 0);printf("%.2f %d", p*pow((1.0+r/100.0), maxdepth), cnt);return 0;
}

 

转载于:https://www.cnblogs.com/mr-stn/p/9139221.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/397761.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql 列数据显示转成行数据显示_Mysql的列修改成行并显示数据的简单实现

创建测试表&#xff1a;DROP TABLE IF EXISTS test;CREATE TABLE test (year int(11) DEFAULT NULL,month int(11) DEFAULT NULL,amount double DEFAULT NULL) ENGINEInnoDB DEFAULT CHARSETutf8;插入数据&#xff1a;INSERT INTO test VALUES (1991, 1, 1.1);INSERT INTO test…

Android两种常见错误(ANR和FC)解决办法

ANR(Activity Not Respone)(无响应)先介绍下Main线程&#xff08;也称为UI线程、主线程&#xff09;功能: 1.创建UI控件2.更新UI控件状态3.事件处理限制&#xff1a;Main线程不建议有超过5秒的事件出现条件&#xff1a;当用户输入事件5s内没有得到响应&#xff0c;将弹出ANR对话…

mysql命令(command)

连接mysql命令: mysql -h 192.168.1.1 -P 3306 -uuserName -pPassword 显示表的索引: SHOW INDDEX FROM table_name 查看mysql的超时时间&#xff1a;SHOW GLOBAL VARIABLES LIKE %timeout% 备份表结构和表数据&#xff1a;mysqldump -u用户名 -p 库名 表1 表2 > xxx.sql只…

微信5.0登录提示服务器繁忙,iOS集成友盟社会化分享微信无法登录?

iOS集成友盟社会化分享SDK-5.0点击微信登录的时候出现无法获取accessToken的现象&#xff0c;其他如QQ、微博都可以正常登录使用。另外QQ、微博和微信分享都可以正常使用。望各位早日帮我解决或者分析一下。谢谢//微信登录之后的回调- (BOOL)application:(UIApplication *)appl…

sql获取某列出现频次最多的值_业务硬核SQL集锦

戳上方蓝字关注我 这两年学会了跑sql&#xff0c;当时有很多同学帮助我精进了这个技能&#xff0c;现在也写成一个小教程&#xff0c;反馈给大家。适用对象&#xff1a;工作中能接触到sql查询平台的业务同学(例如有数据查询权限的产品与运营同学)适用场景&#xff1a;查询hive&…

void ,NULL与0的区别联系

void ,NULL及0的区别联系 void的详解: void的字面意思是“无类型”或“空类型”&#xff0c;void*则为“无针型指针”&#xff0c;那就意味着void*可以指向任何类型的数据。 众所周知&#xff0c;如果指针p1和p2的类型相同&#xff0c;那么我们可以直接在p1和p2间互相赋值&…

python 2 days

1&#xff0c;格式化输出&#xff0c;%s %d 2&#xff0c;复习昨日讲题 编译型&#xff1a; 将代码一次性全部编译成二进制&#xff0c;然后运行。 优点&#xff1a;执行效率高。 缺点&#xff1a;开发效率低&#xff0c;不能跨平台。 C解释型&#xff1a; 代码…

nginx编译安装与配置使用

第一部分----nginx基本应用源码编译安装nginx1、安装pcre软件包&#xff08;使nginx支持http rewrite模块&#xff09;yum install -y pcre yum install -y pcre-devel2、安装openssl-devel&#xff08;使nginx支持ssl&#xff09;yum install -y openssl-devel3、创建用户ngin…

ubuntu+查看服务器文件夹权限,Ubuntu - 文件夹权限查看与修改

Ubuntu 文件的归属身份有四种&#xff1a;u - 拥有文件的用户(所有者)g - 所有者所在的组群o - 其他人(不是所有者或所有者的组群)a - 每个人或全部(u, g, o)1. 查看文件/文件夹权限ls -l filename # 查看文件权限ls -ld folder # 查看文件夹权限输出结果如&#xff1a;drwxrwx…

mysql dump 1449_跨版本mysqldump恢复报错Errno1449

已经有一套主从mysql,新增两个slave主库Server version: 5.6.22-log MySQL Community Server (GPL)旧从库Server version: 5.6.28-log MySQL Community Server (GPL)新增SLAVE 1&#xff1a; Server version: 5.6.22-log MySQL Community Server (GPL)新增SLAVE 2&#xff1a; …

修复 Xcode 错误 “The identity used to sign the executable is no longer valid”

如图&#xff1a; 解决方法来自&#xff1a;http://stackoverflow.com/questions/7088441/the-identity-used-to-sign-the-executable-is-no-longer-valid/14275197 Restarting Xcode didnt work for me. What fixed it for me was going to Accounts in Xcode (in preferences…

centos设置ip

这里是centos7.vmware安装centos后需要设置ip 1.首先查看虚拟机的网络适配器信息 2.根据信息修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens33 图为修改后的,最初的配置为 BOOTPROTOdhcp ONBOOTno IPADDR,GATEWAY,NETMASK没有进行配置需要根据网络适配器配置手动维…

微信支付+服务器+php代码,php 微信支付企业付款(示例代码)

/*** 格式化参数格式化成url参数*/public function ToUrl($arr){$buff "";foreach ($arr as $k > $v){if($k ! "sign" && $v ! "" && !is_array($v)){$buff . $k . "" . $v . "&";}}$buff trim($b…

Spark踩坑记——数据库(Hbase+Mysql)转

转自&#xff1a;http://www.cnblogs.com/xlturing/p/spark.html 前言 在使用Spark Streaming的过程中对于计算产生结果的进行持久化时&#xff0c;我们往往需要操作数据库&#xff0c;去统计或者改变一些值。最近一个实时消费者处理任务&#xff0c;在使用spark streaming进行…

解决Failed to connect session for conifg 故障

服务器升级openssh之后jenkins构建报错了&#xff0c;报错信息如下&#xff1a; Failed to connet or change directory jenkins.plugins.publish_over.BapPublisherException:Failed to connect session for config.....Message [Algorithm negotiation fail] 升级前ssh版本&a…

78oa mysql_78oa系统版本升级方法

可升级版本预览升级方法&#xff1a;1、备份数据库、附件目录、二次开发程序打开开始菜单——控制面板——管理工具——服务&#xff0c;右键点击停止 78oa mysql service 服务&#xff0c;完整复制【D:\78OA\server\modules\storage\data\78oa】(数据库)文件夹至备份区域。完整…

Excel导出显示服务器意外,C# 调用Excel 出现服务器出现意外状况. (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT)...

C# 调用Excel 出现服务器出现意外状况. (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT)htmlprivate Microsoft.Office.Interop.Excel.Application xApp;private Microsoft.Office.Interop.Excel.Workbook xBook;服务器//变量xApp new Microsoft.Office.Interop.Excel.Appl…

列表、元组、字典、集合的定义、操作与综合练习

l[A,B,C] t{A,B,C}l.append(B)print(l)scores[66,77,88]d{A:66,B:77,C:88} d[B]99 d[D]111 d.pop(C) print(d)s1{A,B,C} s2{A,C,D} print(s1&s2) print(s1|s2) 转载于:https://www.cnblogs.com/chenjunyu666/p/9147417.html

xargs

find /tmp/ -name "*.log" -mtime 4 | xargs -i -t mv {} /home/ find /tmp/ -name "*.log" -mtime 4 -print0 | xargs -0 rm -f xargs(1) xargs是给命令传递参数的一个过滤器&#xff0c;也是组合多个命令的一个工具。它把一个数据流分割为一些足够小的块…

export mysql home_mysql的Linux下安装笔记

注&#xff1a;在5.7之后MySQL不在生成my-default.cnf配置。tar -xzvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gzmv mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz/ /usr/local/mysql新建 useradd mysql新建文件夹mkdir /usr/local/mysql/data生成配置&#xff1a;./mysqld -…