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 6Sample Output:
1.85 2
自己的写法,思路比较简单,按父结点进行存储,但是回溯找父结点的过程涉及重复计算,3个用例超时了

#include<cstdio>
#include<cmath>
int n;
double p, r;
const int maxn = 100010;
int father[maxn];
double highest_price;
int num = 0, depth = 0, max_depth = 0;
double FindHighest(int root) {
for (int i = 0; i < n; i++) {
depth = 0;
int index = i;
while (father[index] != -1) {
index = father[index];
depth++;
}
if (depth > max_depth) {
max_depth = depth;
num = 1;
}
else if (depth == max_depth) {
num++;
}
}
return p * pow((1 + r), max_depth);
}
int main() {
scanf("%d%lf%lf", &n, &p, &r);
r /= 100;
int root;
for (int i = 0; i < n; i++) {
scanf("%d", &father[i]);
if (father[i] == -1)
root = father[i];
}
highest_price = FindHighest(root);
printf("%.2f %d", highest_price, num);
return 0;
}
重构,还原树+DFS,顺利通过
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
int n;
double p, r;
const int maxn = 100010;
vector<int> child[maxn];
double highest_price;
int num = 0, max_depth = 0;
void DFS(int index, int depth) {
if (child[index].size() == 0) {
if (depth > max_depth) {
num = 1;
max_depth = depth;
}
else if(depth == max_depth) num++;
return;
}
for (int i = 0; i < child[index].size(); i++) {
DFS(child[index][i], depth + 1);
}
}
int main() {
scanf("%d%lf%lf", &n, &p, &r);
r /= 100;
int root;
for (int i = 0; i < n; i++) {
int tmp;
scanf("%d", &tmp);
if (tmp != -1)
child[tmp].push_back(i); //i是tmp的子节点
else {
root = i;
}
}
DFS(root, 0);
printf("%.2f %d", p * pow((1 + r), max_depth), num);
return 0;
}
供应链成本计算

本文探讨了供应链中产品从供应商到零售商的价格变化规律,介绍了一种计算供应链中最高价格及销售此价格产品的零售商数量的方法。通过存储父节点信息并采用深度优先搜索(DFS),实现了对供应链树的构建与遍历,从而找出最深的供应链路径,以此计算最高价格。
493

被折叠的 条评论
为什么被折叠?



