Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 题解 » 

PAT 1090. Highest Price in Supply Chain

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 <cmath>
#include <vector>
using namespace std;

const int mx = 100000 + 10;
vector<int> g[mx]; //图的邻接表
int n, root, num;
double p, r, ans;

void dfs(int x, int depth)
	if (g[x].empty())
		double tem = p * pow(1 + r/100, double(depth));
		if (fabs(tem - ans) < 1e-6) num++;
		else if(tem > ans) ans = tem, num = 1;
	for (int i = 0; i < g[x].size(); i++) dfs(g[x][i], depth + 1);

int main()
	scanf("%d%lf%lf", &n, &p, &r);
	for (int i = 0; i < n; i++)
		int t; scanf("%d", &t);
		if (t == -1) root = i;
		else g[t].push_back(i);
	dfs(root, 0);
	printf("%.2lf %d\n", ans, num);
	return 0;
上一篇: 下一篇: