PAT甲级——1106 Lowest Price in Supply Chain(BFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90444872
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 (≤), 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 1.
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
题目大意:在一个供应链(有向无权图)里,从供应商(源点)开始,每经过一个中间商就会加一次价格( P*(1+r%) ),消费者只能从零售商(叶节点)处购买产品,供应链不会形成环,求消费者最终可以拿到的最少的价位以及持此价格的零售商的个数。
思路:有很多方法,比如把供应链当成一棵树,找深度最浅的叶节点;我是把它作为一个有向无权图来考虑的,不管什么方法,总归是BFS或者DFS寻找叶节点,我使用的BFS(少用递归比较保险,毕竟要卡时间),把每个节点到源点的距离都算出来了,按理说找到最近的零售商就可以停止搜索了,但是考虑到测试点大概率会有遍历整张图的样例,所以说没必要优化了(其实是懒~),然后将零售商的距离排个序,找出最近的几个,算一下价格~
注意:价格变量要用double,因为float的精度不够,会卡住三个测试点。。
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
vector <int> G[];
void BFS(vector <int> &dist);
int main()
{
int N, M, K;
double P, r;//注意,float的精度不够,会卡住三个测试点!!!
scanf("%d%lf%lf", &N, &P, &r);
vector <int> dist(N, -), retailer;
for (int i = ; i < N; i++) {
scanf("%d", &K);
if (K == ) {
retailer.push_back(i);//记录零售商
continue;
}
int u;
for (int j = ; j < K; j++) {
scanf("%d", &u);
G[i].push_back(u);
}
}
BFS(dist);
vector <int> rDist;
for (int i = ; i < retailer.size(); i++)
rDist.push_back(dist[retailer[i]]);//记录零售商的加价程度,也就是price的系数的指数
sort(rDist.begin(), rDist.end());//排序
int ansDist = rDist[], cnt = ;
for (int i = ; i < rDist.size(); i++) {
if (rDist[i] > ansDist)
break;
cnt++;
}
double price = pow( + r / 100.0, ansDist)*P;
printf("%.4lf %d\n", price, cnt);
return ;
}
void BFS(vector <int> &dist) {
int u, v;
queue <int> Q;
Q.push();
dist[] = ;
while (!Q.empty()) {
u = Q.front();
Q.pop();
for (int i = ; i < G[u].size(); i++) {
v = G[u][i];
if (dist[v] == -) {
dist[v] = dist[u] + ;
Q.push(v);
}
}
}
}
PAT甲级——1106 Lowest Price in Supply Chain(BFS)的更多相关文章
- PAT 甲级 1106 Lowest Price in Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464 A supply chain is a ne ...
- PAT甲级——A1106 Lowest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...
- [建树(非二叉树)] 1106. Lowest Price in Supply Chain (25)
1106. Lowest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors( ...
- PAT 1106 Lowest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 1106. Lowest Price in Supply Chain (25)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT 甲级 1090 Highest Price in Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...
- PAT甲级——A1090 Highest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 【PAT甲级】1106 Lowest Price in Supply Chain (25分)
题意:输入一个正整数N(<=1e5),两个小数P和R,分别表示树的结点个数和商品原价以及每下探一层会涨幅的百分比.输出叶子结点深度最小的商品价格和深度最小的叶子结点个数. trick: 测试点1 ...
随机推荐
- HDU4825 Xor Sum —— Trie树
题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- CentOS7 默认防火墙firewalld
firewalld基础 firewalld是CentOS7源生支持的防火墙,firewalld最大的好处有两个:支持动态更新,不用重启服务:第二个就是加入了防火墙的“zone”概念. firewall ...
- tensorflow 实现逻辑回归——原以为TensorFlow不擅长做线性回归或者逻辑回归,原来是这么简单哇!
实现的是预测 低 出生 体重 的 概率.尼克·麦克卢尔(Nick McClure). TensorFlow机器学习实战指南 (智能系统与技术丛书) (Kindle 位置 1060-1061). Kin ...
- PS 滤镜— — 万花筒效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- java面试题04
1.就你所熟悉的银行业务面说一下,越详细越好 银行经验:手机银行 网上银行经验 怎么支付 转账 了解基本业务 2.了解工作流的控制,审批流程以及帐务处理么? java中怎么实现工作流.审批流程 ...
- iOS UINavgationController、 UINavigationBar、 UINavigationItem关系分析
一般导航控制器含有4个对象,UINavigationController.UINavigationBar.UIViewController.UINavigationItem. 1:UINavigati ...
- bzoj 2850: 巧克力王国 K-D树
题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=2850 题解 对于每个人,我们发现它能够接受的巧克力中 如果对参数分别讨论,那么一定是一个连 ...
- QT(3)第一个QT程序
一.创建一个空项目 二.配置 在demo.pro文件中添加配置: greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 添加main.cpp文件 三.编写代码 ...
- [51nod1035]最长的循环节
题意:输出<=n的数中倒数循环节长度最长的那个数 解题关键:http://w3.math.sinica.edu.tw/math_media/d253/25311.pdf https://wenk ...
- iview组件select之默认展示label,并传空value做方法入参
要求: 默认查询操作日期在当日的数据:(打开页面时默认选中时间.全部) 后台约定:选定“全部”这个条件,传的值是空"" 综上:使用select选择框的v-model绑定数据,使用: ...