PAT甲级 1106 Lowest Price in Supply Chain (25分)

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464

思路:

1.考察树的dfs;
2.零售商只可能在叶子节点,若从root到零售商所经历的边的数量为 a a a,则该零售商的价格就是 p ∗ ( 1 + r ∗ 0.01 ) a p*(1+r*0.01)^a p(1+r0.01)a

代码:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;
int n, cnt;
double p, r, ans = 10e10;
bool isr[maxn];
vector<int> chd[maxn];

void dfs(int id, double np) {
    
    
	if(isr[id]) {
    
    
		if(np == ans) ++cnt;
		if(np < ans) {
    
    
			cnt = 1;
			ans = np;
		}
		return;
	}
	for(int &x : chd[id]) dfs(x, np * r);
}

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	scanf("%d%lf%lf", &n, &p, &r);
	r = 1 + r / 100.0;
	for(int i = 0; i < n; ++i) {
    
    
		int k;
		scanf("%d", &k);
		for(int j = 0; j < k; ++j) {
    
    
			int x;
			scanf("%d", &x);
			chd[i].push_back(x);	
		}
		if(!k) isr[i] = true;
	}
	dfs(0, p);
	printf("%.4f %d", ans, cnt);
	return 0;	
}

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/107775357