PAT A1079 Total Sales of Supply Chain供应链总销售额 [树的遍历 深度优先]

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 total sales from all the retailers.

一个供应链是一个零售商、经销商和供应商(任何一个参与产品从供应到顾客的人)的网络,从一个根源供应商开始,供应链上的每一个人从他们的供应者那里以P价格买入产品,然后以高于P r%的价格卖掉。只有零售商会面对顾客。假设供应链上的每一个成员除了根源供应商外只有一个供应者,没有供应回路。

现在给出一个供应链,你需要得到所有零售商的总销售额。

Input Specification:

Each input file contains one test case. For each case, the first line contains three positive numbers: N (≤10​^5), 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 unit 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:

K​i​​ ID[1] ID[2] ... ID[K​i​​]

where in the i-th line, K​i​​ 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. K​j​​ being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after K​j​​. All the numbers in a line are separated by a space.

第一行包含三个正整数:N--供应链上的成员总数(ID从0-N-1,根源供应商ID是0)

                                       P--根源供应商给出的价格

                                       r--价格增长率

后面N行每一行都描述了一个经销商和零售商,格式如下 Ki  ID[1]  ID[2]  ....ID[Ki]。在第i行,ki是从供应者i处获得商品的经销商和零售商总数,后面写出了他们的ID。Kj为0意味着第j个成员是零售商,随后会显示出产品的总量。

Output Specification:

For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will not exceed 10​^10​​.

打印出我们能从零售商那里得到的总销售额,精确到小数点后1位

算法笔记中的总结:

#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=100010;
struct node{
	double data;//数据域(存放货物量) 
	vector<int> child;//指针域 
}Node[maxn];

int n;
double p,r,ans=0;//ans为叶节点货物的价格之和

//-------深度优先遍历---------- 
void DFS(int index,int depth){
	if(Node[index].child.size()==0){//到达叶节点
		ans+=Node[index].data*pow(1+r,depth);//累加叶节点货物价格 
		return; 
	}
	for(int i=0;i<Node[index].child.size();i++){
		DFS(Node[index].child[i],depth+1);
	}
} 

int main(){
	int k,child;
	scanf("%d%lf%lf",&n,&p,&r);
	r /= 100;
	//构造供应树 
	for(int i=0;i<n;i++){
		scanf("%d",&k);
		if(k==0){//叶节点标记 
			scanf("%lf",&Node[i].data);
		}else{
			for(int j=0;j<k;j++){
				scanf("%d",&child);
				Node[i].child.push_back(child);
			}
		}
	}
	DFS(0,0);
	printf("%.1f\n",p*ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38179583/article/details/86133843