PAT 1079 Total Sales of Supply Chain

news/2024/3/29 14:38:42/文章来源:https://blog.csdn.net/horsetaill/article/details/132309083

个人学习记录,代码难免不尽人意。
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 total sales from all the 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 unit 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 jbeing 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after K j. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will not exceed 1010.

Sample Input:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3
Sample Output:
42.4

#include <cstdio>
#include<queue>
#include<string>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;struct node{int data;vector<int> child;double amount;int level;
}; 
node* Node[100010];
node* newnode(int data){node* root =new node;root->data=data;root->child.clear();root->amount=0;root->level=-1;return root;
}
void level(int root){queue<int> q;q.push(root);Node[root]->level=0;while(!q.empty()){int now=q.front();q.pop();if(!Node[now]->child.empty()){for(int i=0;i<Node[now]->child.size();i++){int a=Node[now]->child[i];q.push(a);Node[a]->level=Node[now]->level+1;}}}
}
int main(){int n;double p,r;scanf("%d%lf%lf",&n,&p,&r);for(int i=0;i<n;i++){Node[i]=newnode(i);}for(int i=0;i<n;i++){int type;scanf("%d",&type);if(type==0){scanf("%lf",&Node[i]->amount);}else{for(int j=0;j<type;j++){int a;scanf("%d",&a);Node[i]->child.push_back(a);}}}level(0);queue<node* > q;q.push(Node[0]);double sum=0;while(!q.empty()){node* no=q.front();q.pop();if(!no->child.empty()){for(int i=0;i<no->child.size();i++){q.push(Node[no->child[i]]);}}else{double rate=1+(r/100);sum+=p*pow(rate,no->level)*no->amount;}}printf("%.1lf\n",sum);
}

这道题还是蛮简单的,就是我看到题目中说数据不超过1010,觉得肯定会有数据溢出的测试点,本想着看到错误再改代码的,结果直接通过了,也算是轻松吧!
后来查资料发现,double基本不会溢出,基本可以放心。
在这里插入图片描述

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

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

相关文章

九耶丨阁瑞钛伦特-请说说你在工作中的PRD文档是如何撰写的?

1、背景说明&#xff08;解释清楚为什么要做这样一件事&#xff0c;以及做这件事的价值&#xff0c;先把观点拉齐&#xff0c;才方便接下来的工作开展&#xff09; 简要介绍与项目相关的背景信息、项目要满足的用户需求、开展项目的主要原因、项目期望上线时间、项目涉及的具体…

YOLOV7改进:加入RCS-OSA模块,提升检测速度

1.该文章属于YOLOV5/YOLOV7/YOLOV8改进专栏,包含大量的改进方式,主要以2023年的最新文章和2022年的文章提出改进方式。 2.提供更加详细的改进方法,如将注意力机制添加到网络的不同位置,便于做实验,也可以当做论文的创新点。 2.涨点效果:RCS-OSA模块更加轻量化,有效提升检…

OpenCV-Python中的图像处理-图像直方图

OpenCV-Python中的图像处理-图像直方图 图像直方图统计直方图绘制直方图Matplotlib绘制灰度直方图Matplotlib绘制RGB直方图 使用掩膜统计直方图直方图均衡化Numpy图像直方图均衡化OpenCV中的直方图均衡化CLAHE 有限对比适应性直方图均衡化 2D直方图OpenCV中的2D直方图Numpy中2D…

【11】Redis学习笔记 (微软windows版本)【Redis】

注意:官redis方不支持windows版本 只支持linux 此笔记是依托微软开发windows版本学习 一、前言 Redis简介&#xff1a; Redis&#xff08;Remote Dictionary Server&#xff09;是一个开源的内存数据结构存储系统&#xff0c;它也被称为数据结构服务器。Redis以键值对&am…

UNIAPP中开发企业微信小程序

概述 需求为使用uni-app开发企业微信小程序。希望可以借助现成的uni-app框架&#xff0c;快速开发。遇到的问题是uni-app引入jweixin-1.2.0.js提示异常: Reason: TypeError: Cannot read properties of undefined (reading ‘title’)。本文中描述了如何解决该问题&#xff0c…

UCLA发布SciBench,评估大语言模型的科学问题解决能力

©PaperWeekly 原创 作者 | Xiaoxuan Wang 单位 | UCLA 研究方向 | 大语言模型评测 论文题目&#xff1a; SciBench: Evaluating College-Level Scientific Problem-Solving Abilities of Large Language Models 论文链接&#xff1a; https://arxiv.org/abs/2307.10635 代…

mysql 8.0.20不停机主从同步

一、环境 CentOS &#xff1a; 7.3.1611 (Core) mysql&#xff1a;8.0.20 二、遇到的问题 1.查看主从同步发现下列问题 error connecting to master repl192.168.0.21:3306 - retry-time: 60 retries: 4 message: Authentication plugin caching_sha2_password reported e…

springboot多模块打包方式

明确子父模块结构 父目录是带modules 大致结构如下&#xff1a; <modules><module>ruoyi-admin</module><module>ruoyi-framework</module><module>ruoyi-system</module><module>ruoyi-quartz</module><module>…

【C++从0到王者】第二十一站:继承

文章目录 前言一、继承的概念及定义1. 继承的概念2.继承的格式3.继承关系与访问限定符 二、基类和派生类的赋值转换三、继承中的作用域四、派生类的默认成员函数五、继承与友元六、继承与静态成员 前言 继承是面向对象的三大特性之一。我们常常会遇到这样的情况。很多角色的信…

MYSQL 作业三

创建一个student表格&#xff1a; create table student( id int(10) not null unique primary key, name varchar(20) not null, sex varchar(4), birth year, department varchar(20), address varchar(50) ); 创建一个score表格 create table score( id int(10) n…

ASP.NET WEB API通过SugarSql连接MySQL数据库

注意&#xff1a;VS2022企业版可以&#xff0c;社区版可能存在问题。实体名称和字段和数据库中的要一致。 1、创建项目&#xff0c;安装SqlSugarCore、Pomelo.EntityFrameworkCore.MySql插件 2、文件结构 2、appsettings.json { “Logging”: { “LogLevel”: { “Default”: …

从零实现kv存储V2.0

在V1.0版本&#xff0c;我们实现了基于array的kv存储引擎。本文继续完善&#xff0c;增加rbtree、hash、skiptable引擎。 实际上&#xff0c;在框架确定的基础上&#xff0c;其他的引擎只需要添加接口即可。 一、架构设计 二、具体实现 2.1 引擎层 //---------------------…

每天一道leetcode:646. 最长数对链(动态规划中等)

今日份题目&#xff1a; 给你一个由 n 个数对组成的数对数组 pairs &#xff0c;其中 pairs[i] [lefti, righti] 且 lefti < righti 。 现在&#xff0c;我们定义一种 跟随 关系&#xff0c;当且仅当 b < c 时&#xff0c;数对 p2 [c, d] 才可以跟在 p1 [a, b] 后面…

WSL2 Ubuntu子系统安装OpenCV

文章目录 前言一、&#xfeff;基本概念二、操作步骤1.下载源码2.安装依赖3.运行编译4.配置路径 前言 OpenCV用C语言编写&#xff0c;它的主要接口也是C语言&#xff0c;但是依然保留了大量的C语言接口。该库也有大量的Python, Java and MATLAB/OCTAVE (版本2.5)的接口。这些语…

最长递增子序列——力扣300

int lengthOfLIS(vector<int>& nums) {int len=1, n=nums.size();if

临床试验三原则-对照、重复、随机

临床试验必须遵循三个基本原则&#xff1a;对照、重复、随机。 一、对照原则和对照的设置 核心观点&#xff1a;有比较才有鉴别。 对照组和试验组同质可比。 三臂试验 安慰剂&#xff1a;试验组&#xff1a;阳性对照组1&#xff1a;n&#xff1a;m&#xff08;n≥m&#xff…

Android Framework 动态更新插拔设备节点执行权限

TF卡设备节点是插上之后动态添加&#xff0c;所以不能通过初始化设备节点权限来解决&#xff0c;需要监听TF插入事件&#xff0c;在init.rc 监听插入后动态更新设备节点执行权限 添加插拔TF卡监听 frameworks/base/services/core/java/com/android/server/StorageManagerServic…

轻量级自动化测试框架WebZ

一、什么是WebZ WebZ是我用Python写的“关键字驱动”的自动化测试框架&#xff0c;基于WebDriver。 设计该框架的初衷是&#xff1a;用自动化测试让测试人员从一些简单却重复的测试中解放出来。之所以用“关键字驱动”模式是因为我觉得这样能让测试人员&#xff08;测试执行人员…

《Java-SE-第三十八章》之注解

前言 在你立足处深挖下去,就会有泉水涌出!别管蒙昧者们叫嚷:“下边永远是地狱!” 博客主页&#xff1a;KC老衲爱尼姑的博客主页 博主的github&#xff0c;平常所写代码皆在于此 共勉&#xff1a;talk is cheap, show me the code 作者是爪哇岛的新手&#xff0c;水平很有限&…

Hlang社区项目说明

文章目录 前言Hlang社区技术前端后端 前言 Hello,欢迎来到本专栏&#xff0c;那么这也是第一次做这种类型的专栏&#xff0c;如有不做多多指教。那么在这里我要隆重介绍的就是这个Hlang这个项目。 首先&#xff0c;这里我要说明的是&#xff0c;我们的这个项目其实是分为两个…