题意
PAT A1079 Total Sales of Supply Chain (25 分)
PAT A1090 Highest Price in Supply Chain (25 分)
与这两题类似,只不过改成了输出最小的零售价
注意点
同上两题
题解
解题思路:
①定义数的结点,因为不需要数据域,直接用vector数组定义
②读入每个结点的孩子结点,创建静态树
③DFS函数,到最深处返回,如果小于最小路径则覆盖
④输出结果
#include <stdio.h>
#include <vector>
#include <math.h>
using namespace std;
const int maxn = 100010;
//定义结点
vector<int> Node[maxn];
//变量
int n;
double uniPrice, rate;
//DFS
int minPath = maxn, count = 0;
void DFS(int index, int num) {
if (Node[index].size() == 0) {
if (num < minPath) {
minPath = num;
count = 1;
} else if (num == minPath) {
count++;
}
}
for (int i = 0; i < Node[index].size(); i++) {
DFS(Node[index][i], num+1);
}
}
int main() {
scanf("%d%lf%lf", &n, &uniPrice, &rate);
for (int i = 0; i < n; i++) {
int childNum;
scanf("%d", &childNum);
for (int j = 0; j < childNum; j++) {
int temp;
scanf("%d", &temp);
Node[i].push_back(temp);
}
}
DFS(0, 0);
printf("%.4f %d\n", uniPrice * pow((100+rate)*0.01, minPath), count);
return 0;
}