1. 程式人生 > >PAT A1079 Total Sales of Supply Chain供應鏈總銷售額 [樹的遍歷 深度優先]

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;
}