PAT (Advanced Level) Practice 1106 Lowest Price in Supply Chain (25 分) 凌宸1642

Posted 凌宸00

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) Practice 1106 Lowest Price in Supply Chain (25 分) 凌宸1642相关的知识,希望对你有一定的参考价值。

PAT (Advanced Level) Practice 1106 Lowest Price in Supply Chain (25 分) 凌宸1642

题目描述:

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.

译:供应链是零售商(零售商)、分销商(经销商)和供应商(供应商)的网络——每个人都参与将产品从供应商转移到客户。

从一个根供应商开始,链上的每个人都以价格 P 从供应商那里购买产品,然后以比 P 高 r% 的价格销售或分销。只有零售商才会面对顾客。 假设供应链中的每个成员除了根供应商外,只有一个供应商,不存在供应周期。

现在给定一个供应链,你应该告诉客户可以从一些零售商那里得到的最低价格。


Input Specification (输入说明):

Each input file contains one test case. For each case, The first line contains three positive numbers: N (≤105), 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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行包含三个正数:N(≤105),供应链中成员的总数(因此他们的 ID 从 0 到 N-1 编号,根供应商的 ID 为 0) ; P,根供应商给出的价格; 和 r,每个分销商或零售商的价格增长百分比。 然后是 N 行,每行描述一个分销商或零售商,格式如下:

Ki ID[1] ID[2] ... ID[Ki]

其中在第 i 行中,Ki 是从供应商 i 接收产品的分销商或零售商的总数,然后是这些分销商或零售商的 ID。 Kj 为 0 表示第 j 个成员是零售商。 一行中的所有数字都用空格分隔。


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 1010.

译:对于每个测试用例,在一行中打印出我们可以从一些零售商那里得到的最低价格,精确到小数点后 4 位,以及以最低价格销售的零售商数量。 两个数字之间必须有一个空格。 保证所有价格不超过1010


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

The Idea:

首先是建树,然后对二叉树求其最小的叶子结点层号,以及该层有多少个叶子结点,然后按照复利计算售价即可。

  • 建树,由于N的范围很小,可以直接使用结构体来表示节点。
  • 找到根节点,默认为 0。
  • 使用 BFS 进行层序遍历,可以先获得每一层的节点个数,在进行判断,统计该层的叶子结点个数,如果存在叶子结点,则遍历可以终止,也就得到了最小的叶子结点层号以及个数。
  • 然后按照复利计算公式,计算复利以及按要求输出结果。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
vector<int> Tree[maxn] ;
int n;
double p, r;
int cnt = 0, level = 0;
void bfs(int root) {
	queue<int> q;
	q.push(root);
	while (!q.empty()) {
		level++;
		int size = q.size();
		for (int i = 0; i < size; i++) {//遍历该层的所有结点
			int top = q.front();
			q.pop();
			if (Tree[top].size() == 0) //叶子节点
				cnt ++ ;
			else 
				for (int j = 0; j < Tree[top].size(); j++) 
					q.push(Tree[top][j]) ;
		}
		if (cnt > 0) return ;  //找到叶子节点了就return	
	}
}

int main() {
	cin >> n >> p >> r;
	int k, temp;
	for (int i = 0; i < n; i++) {
		cin >> k;
		for (int j = 0; j < k; j++) {
			cin >> temp;
			Tree[i].push_back(temp);
		}
	}
	bfs(0);
	printf("%.4f %d", p * pow((1 + r / 100), level - 1), cnt);
}

以上是关于PAT (Advanced Level) Practice 1106 Lowest Price in Supply Chain (25 分) 凌宸1642的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095

PAT Advanced Level 1038

pat advanced level 1063

PAT Advanced level 1003 Emergency