本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90444872

1106 Lowest 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. 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:

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.

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)的更多相关文章

  1. PAT 甲级 1106 Lowest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464 A supply chain is a ne ...

  2. PAT甲级——A1106 Lowest Price in Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  3. PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...

  4. [建树(非二叉树)] 1106. Lowest Price in Supply Chain (25)

    1106. Lowest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors( ...

  5. PAT 1106 Lowest Price in Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  6. 1106. Lowest Price in Supply Chain (25)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  7. PAT 甲级 1090 Highest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...

  8. PAT甲级——A1090 Highest Price in Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  9. 【PAT甲级】1106 Lowest Price in Supply Chain (25分)

    题意:输入一个正整数N(<=1e5),两个小数P和R,分别表示树的结点个数和商品原价以及每下探一层会涨幅的百分比.输出叶子结点深度最小的商品价格和深度最小的叶子结点个数. trick: 测试点1 ...

随机推荐

  1. 使用C语言解析URL

    1. [代码]容易写成自己输入URL,这里测试一个例子     #include <stdio.h>#include <stdlib.h>#include <string ...

  2. 搭建LoadRunner中的场景(四)控制器的全局设置

    选择“Tools”菜单-“Options”选项打开设置窗口 1.超时设置 2.运行时设置 3.运行时文件存储设置 4.路径翻译表 路径翻译表是一种映射,将控制器上的文件路径转换为远程主机上的文件路径. ...

  3. openfire调试环境

    导入工程: File->New->project: 选择“Java project from existing ant buildfile” 再从菜单windows->show vi ...

  4. centos7搭建mysql-5.7.22主从复制

    mysql7.7.22主从复制 本项目是根据真实环境搭建编写出文档,文档中的目录也是根据自己公司环境所创建.公司原来是一台服务器搭建的数据库(5.7.22),由于业务的扩展需要搭建一台从服务器,减轻主 ...

  5. 【HDU 6126】Give out candies 最小割

    题意 有$n​$个小朋友,给每个人分$1~m​$个糖果,有k个限制 限制形如$(x,y,z)​$ 表示第$x​$个人分到的糖数减去第$y​$个人分到的糖数不大于$z​$,给第$i​$个人$j​$颗糖获 ...

  6. COM组件宏观认识

    一直搞不清楚COM到底是个什么东西,记录一些个人感想,可能很多错误的,慢慢消化. 一.宏观认识: 1.COM(组件对象模型)是一种标准,规则,要求,即即于建筑设计指标要求. 2.语言无关性,因为是建立 ...

  7. animation-delay负值

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 「LuoguP1429」 平面最近点对(加强版)

    题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 接下来n行:每行两个实数:x y, ...

  9. Tensorflow知识点学习

    1.TensorFlow中Tensor维度理解: (1)对于2维Tensor 0维对应列 1维对应行 (2)维度操作举例: 对于k维的,tf.reduce_sum(x, axis=k-1)的结果是对最 ...

  10. Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)

    前言:本项目基于spring4.x构建,使用ehcache3.5.2和JCache(jsr107规范) 一.依赖 除了ehcache和cache-api外,注意引用spring-context-sup ...