1106 Lowest Price in Supply Chain (25分)
1106分析:
使用dfs找出叶节点最低的层次以及个数。
C++:
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 100010;
struct Node {
vector<int> child;
} nodes[maxn];
int n;
double p, r;
int minDepth = maxn, num;
void dfs(int index, int depth) {
if (nodes[index].child.size() == 0) {
if (depth < minDepth) {
minDepth = depth;
num = 1;
} else if (depth == minDepth) {
num++;
}
return;
}
for (int i = 0; i < nodes[index].child.size(); i++) {
dfs(nodes[index].child[i], depth + 1);
}
}
int main() {
scanf("%d%lf%lf", &n, &p, &r);
r /= 100;
int k, child;
for (int i = 0; i < n; i++) {
scanf("%d", &k);
for (int j = 0; j < k; j++) {
scanf("%d", &child);
nodes[i].child.push_back(child);
}
}
dfs(0, 0);
printf("%.4f %d", p * pow(1 + r, minDepth), num);
}
网友评论