关闭

1079. Total Sales of Supply Chain (25)

标签: PAT树的存储层次遍历BFS
155人阅读 评论(0) 收藏 举报
分类:
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.

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 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:

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, then instead the total amount of the product will be given after Kj. All the numbers in a line are separated by a space.

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 1010.


IDEA

1.树的存储,vecttor<vector<int>> tree(n),置空操作tree[i].clear();

2.用层序遍历实现,用到queue


CODE

#include<iostream>
#include<vector>
#include<queue>
#include<iomanip>
#include<fstream>
using namespace std;
int main(){
//	freopen("input.txt","r",stdin);
	int n;
	double price,r;
	cin>>n>>price>>r;
	
	vector< vector<int> > tree(n);
	vector<int> retailer(n,0);
	
	for(int i=0;i<n;i++){
		int k;
		cin>>k;
		int num;
		if(k==0){
			cin>>num;
			retailer[i]=num;
			tree[i].clear();
		}else{
			for(int j=0;j<k;j++){
				cin>>num;
				tree[i].push_back(num);
			}
		}
	}
//	vector<int>::iterator it;
//	for(it=retailer.begin();it!=retailer.end();it++){
//		cout<<*it<<" ";
//	}

	queue<int> que;
	int root=0;
	que.push(root);
	
	int last=root,tail=root;
	double factor=price;
	double sum=0;
	while(!que.empty()){
		int tmp=que.front();
		que.pop();
		sum+=retailer[tmp]*factor;
		for(int i=0;i<tree[tmp].size();i++){
			int num=tree[tmp][i];
			que.push(num);
			tail=num;
		}
		if(tmp==last){
			factor*=(1+r/100);
			last=tail;
		}
	}
	cout<<fixed<<setprecision(1)<<sum;
//	fclose(stdin);
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22367次
    • 积分:2404
    • 等级:
    • 排名:第15516名
    • 原创:230篇
    • 转载:2篇
    • 译文:0篇
    • 评论:5条
    最新评论