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

题目链接:传送门

思路:直接dfs算售价和最小售价叶节点的数量。

代码:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;

bool vis[maxn];
double price[maxn];
vector <int> g[maxn];
double ans , p , r;
int tag;

void dfs(int u) {
	if(!g[u].size() && ans > price[u]) {
		tag = 1;
		ans = price[u];
	}
	else if(!g[u].size() && ans == price[u]){
		tag++;
	}
	for(int i = 0 ; i < g[u].size() ; i++) {
		int v = g[u][i];
		price[v] = price[u] * (1 + r * 0.01);
		dfs(v);
	}
}


int main() {
	int n;
	ios::sync_with_stdio(0);
	cin >> n >> p >> r;
	price[0] = p;
	for(int i = 0 ; i < n ; i++) {
		int k , t;
		cin >> k;
		while(k--) {
			cin >> t;
			g[i].push_back(t);
		}
	}
	ans = 1e10 + 5;
	dfs(0);
	printf("%.4f %d\n" , ans , tag);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39475280/article/details/102945876