PAT 甲级 1106 Lowest Price in Supply Chain (DFS)(c++版)(附代码注释)(附题意分析)

1106 Lowest Price in Supply Chain (25 分)

原文链接:http://kakazai.cn/index.php/Kaka/Pat/query/id/196

题目

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464

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:

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 10^10.

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%,每个经销商和零售商只有一个上级。

求出最低的零售价,最低零售价的零售商数量。

  • 分析

可将供应体系看成一棵树,唯一的供应商是根结点,经销商是非叶结点,零售商是叶结点,每个结点都有一个价格,且该价格是其父结点价格的1+r%。

可用DFS遍历所有叶结点,并求出其价格,统计最小值。

知识点与坑点

  • 知识点

1)DFS

  • 坑点

1)

一、DFS

算法思路

1 遍历所有结点,求出每个结点的价格,结点价格为父结点的1+r%

2 遍历的同时,遇到叶结点,不断更新最低零售价,以及统计其个数

3 输出最低零售价,以及其个数

代码-c++版
#include<vector>
#include<cstdio>
using namespace std;
const int maxn = 100001;	//最多有100001个结点 
const double INF = 10000000000;	//10的10次方

/* 关键变量 */ 
vector<int> child[maxn];	//存储结点的孩子 
double minp = INF; 	//最低的零售价
double r; //价格递增百分点 
int minpn = 0;	//零售价最低的零售商的数量 

 /* DFS:遍历以求得所有叶结点,以及它们的价格*/ 
void dfs(int cur, double p) {	//当前结点cur,它的价格是p
	//cur是零售商 
    if (child[cur].size() == 0) {	
        if (p < minp) {	
            minp = p;	// 更新最低价格
            minpn = 1;	// 该最低价的零售商目前有1个 
        }
        else if (p == minp) {
            minpn++;	//该最低价的零售商目前多了1个
        }
    }
    //cur不是零售商,访问它的下级 
    for (int i = 0; i < child[cur].size(); i++) {
        int id = child[cur][i];
        dfs(id, p*(1 + r));
    }
}
int main() {
    int n;	//总结点数
	double p; //根结点价格
    scanf("%d %lf %lf", &n, &p, &r);
    r = r / 100;
    
    /* 用变长数组存储各结点的孩子*/
    int num, in1;	//孩子个数,孩子结点 
    for (int i = 0; i < n; i++) {
        scanf("%d", &num);
        for (int j = 0; j < num; j++) {
            scanf("%d", &in1);
            child[i].push_back(in1);
        }
    }
  
	 /* 用DFS求出所有零售商,并找出最低零售价*/
    dfs(0, p);	//供应商即根结点,编号是0,价格是p

	 /* 输出最低零售价,最低零售价的零售商数目*/
    printf("%.4lf %d\n", minp, minpn);
    
    return 0;
}
代码-python版

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值