pat 1090 Highest Price in Supply Chain

本文介绍了一个供应链中计算从供应商到零售商最高价格的算法。通过深度优先搜索遍历供应链网络,考虑价格随层级增加的百分比变化,找出最高售价及其出现次数。

1090. Highest Price in Supply Chain (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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. 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 highest price we 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 (<=105), the total number of the members in the supply chain (and hence they are numbered from 0 to N-1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si is the index of the supplier for the i-th member. Sroot for the root supplier is defined to be -1. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:

1.85 2

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

#define MAX 100010

int n, maxt = 0, cnt = 0;
double P, r, ansPrice;
int tree[MAX], level[MAX];
vector<int> chl[MAX];

void dfs(int index, int high, double price)
{
	int i;
	if(high >= maxt)
	{
		if(high > maxt) maxt = high, cnt = 1, ansPrice = price;

		else if(high == maxt) cnt++, ansPrice = price;
	}

	for(i = 0; i < chl[index].size(); i++)
	{
		int t = chl[index][i];
		level[t] = high + 1;
		double pp = price * (1 + r/100.0);
		dfs(t, level[t], pp);
		
	}
}
int main()
{
	int i, tmp, root;

	while(scanf("%d %lf %lf", &n, &P, &r) != EOF)
	{
		memset(level, 0, sizeof(level));
		
		for(i = 0; i < n; i++)
			tree[i] = -1;

		for(i = 0; i < n; i++)
		{
			scanf("%d", &tmp);

			tree[i] = tmp;
			chl[tmp].push_back(i);

			if(tmp == -1)
				root = i;
		}
		
		level[root] = 1;

		dfs(root, 1, P);
		
		printf("%.2lf %d\n", ansPrice, cnt);
	}
	return 0;	
}


<think>好的,我现在需要处理用户关于PAT 1090题目的查询。首先,用户提到的是PAT甲级题目,但提供的引用是1150题,可能与用户的需求不符。我需要先确认PAT 1090的具体内容。 PAT的题目编号通常与考试年份和题目顺序相关,但用户可能记错了编号。例如,1090可能属于甲级或乙级。我需要回想一下常见的PAT题目。常见的甲级题目如1001、1011等,1090可能不是甲级的,可能属于乙级或者用户记错了编号。 接下来,考虑用户提供的引用[1]和[2]都是关于1150题,即旅行商问题的变种。但用户现在需要的是1090的相关信息,这可能存在编号错误。例如,PAT 1090可能对应“Highest Price in Supply Chain”这道题,这道题确实存在,属于甲级题库,考察树的遍历或DFS/BFS的应用。 用户可能想查询供应链中的最高价格问题。这道题的描述通常涉及树结构,计算从根节点到叶子节点的最长路径,并统计这样的路径数目。例如,每个节点代表一个供应商,父节点到子节点的传输会提高价格,求最高价格以及对应的路径数。 因此,我需要确认PAT 1090的题目内容,并给出解答思路。首先,题目输入通常是节点数、根节点编号,然后是每个节点的父节点。然后,根节点的价格给出,每个非根节点的价格是父节点的价格乘以(1 + r%),其中r是给定的比率。需要找到最高的价格以及拥有这个价格的叶子节点数量。 解答方法通常包括构建树结构,使用DFS或BFS遍历所有叶子节点,记录路径的价格,并比较最大值。同时统计最大价格出现的次数。例如,可以用递归DFS,从根节点开始,逐层计算每个子节点的价格,当遇到叶子节点时更新最大值和计数。 代码部分需要注意浮点数精度问题,可能需要用double类型存储价格。此外,需要高效遍历,避免超时。通常,使用邻接表表示树结构,然后进行深度优先搜索即可。 用户可能需要的相关问题是关于树遍历的应用、供应链问题的变种,或者如何优化此类问题的解决方法。例如,如何避免重复计算,如何处理大规模数据等。 最后,生成相关问题时要围绕PAT 1090的考点,比如树遍历、DFS/BFS的应用、动态规划是否适用,或者类似题目的比较,如与供应链相关的其他题目。</think>关于PAT 1090题目的解答与描述: 根据题目编号推断,PAT 1090对应的是**供应链最高价格问题(Highest Price in Supply Chain)**,属于树遍历的典型应用。题目要求从供应链的根节点出发,找到所有叶子节点中价格最高的路径,并统计达到该价格的叶子节点数量[^1]。 ### 问题描述 供应链中,每个节点(供应商)将产品以**固定利润率**传递给下级节点,最终价格计算公式为: $$P = P_{\text{root}} \times (1 + r\%)^d$$ 其中$d$是节点到根节点的深度。要求输出最高价格及其对应的叶子节点数量。 ### 解题思路 1. **树结构构建**:用邻接表存储非叶子节点的子节点列表,根节点通过输入的特殊标识(通常为-1)确定; 2. **深度优先搜索(DFS)**:从根节点出发递归计算每个叶子节点的价格,动态更新最大值和计数器; 3. **浮点数精度处理**:用`double`类型存储价格,避免计算误差。 ### 示例代码 ```python import sys sys.setrecursionlimit(10000) n, root_price, r = map(float, input().split()) n = int(n) tree = [[] for _ in range(n)] root = -1 for i in range(n): parent = int(input()) if parent == -1: root = i else: tree[parent].append(i) max_price = -1.0 count = 0 def dfs(node, current_price): global max_price, count if not tree[node]: # 叶子节点 if current_price > max_price: max_price = current_price count = 1 elif current_price == max_price: count += 1 return for child in tree[node]: dfs(child, current_price * (1 + r / 100)) dfs(root, root_price) print(f"{max_price:.2f} {count}") ``` ### 复杂度分析 - 时间复杂度:$O(n)$,每个节点仅访问一次 - 空间复杂度:$O(n)$,存储树结构的邻接表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值