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. Only the retailers will face the customers. 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 lowest price a customer 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 their ID's are numbered from 0 to N−1, and the root supplier's ID is 0); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:
Ki ID[1] ID[2] ... ID[Ki]
where in the i-th line, Ki is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID's of these distributors or retailers. Kj being 0 means that the j-th member is a retailer. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 1010.
Sample Input:
10 1.80 1.00 3 2 3 5 1 9 1 4 1 7 0 2 6 1 1 8 0 0 0
Sample Output:
1.8362 2
题目大意:给出一个经销商(树),(根节点)为生产商,ID为0,给出生产商出售的价格和比率r,每一级价格都会涨r%
求经销商(叶子节点)最低售价(当然是层级越少越便宜),并算出有多少人卖这么低的价格。
这道题可以简化成是带有层级的层序遍历树,然后找到叶子节点中价格最低的,定义一个累加器计算即可。
要注意double或float与int。
#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
#define MAXN 100010
struct node {
double price;
vector<int> child;
bool isLeaf;
} T[MAXN]; //Tree
double R; //Rate
void BFS(int root){
queue<int> Q;
Q.push(root);
while(!Q.empty()){
int iter = Q.front();
Q.pop();
double child_price = 1.00 * T[iter].price * (1.00 + R * 0.01);
if(!T[iter].child.size()) T[iter].isLeaf = true; //如果没有孩子,就是叶子节点
for(int i = 0; i < T[iter].child.size(); i++){
int child = T[iter].child[i];
T[child].price = child_price;
Q.push(child);
}
}
}
int main(){
int N;
cin >> N >> T[0].price >> R;
for(int i = 0; i < N; i++){
int cnt, tmp;
cin >> cnt;
T[i].isLeaf = false; //初始化ifLeaf
while(cnt--){
cin >> tmp;
T[i].child.push_back(tmp);
}
}
BFS(0);
int cnt = 0;
double min = 1.0 * 0x7fffffff;
for(int i = 0; i < N; i++){
if(T[i].isLeaf == true && T[i].price < min) //是叶子,而且价格更低
cnt = 1, min = T[i].price;
else if(T[i].isLeaf == true && T[i].price == min) //发现一个价格相同的叶子
cnt++;
}
printf("%.4f %d", min, cnt);
return 0;
}