PATA 1079 Total Sales of Supply Chain(25 分)解题报告

1079 Total Sales of Supply Chain(25 分)

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 (≤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.

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

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3

Sample Output:

42.4

作者: CHEN, Yue

单位: 浙江大学

时间限制: 250 ms

内存限制: 64 MB

代码长度限制: 16 KB

 

题目大意是,在一个供应链中,有批发商,中间商,零售商,只有零售商会对普通顾客出售商品,商品的原价是固定,每经过一级经销商,商品的价格会增加t%,题目会给出美国零售商的货物数量,要求你求出所有在售商品的价值总和,在输入中先给出结点数量n,然后在接下来的n行中,依次在每一行给出0~n-1号结点的子节点数量,子节点编号,如果子节点数量为0,那么说明该结点是叶节点,本行第二个数是零售商的货物数量

解题思路:这个题我想了一下大概有两种解法,第一是用建树+层序遍历,在树节点中记录一个level变量来记录结点层次(根节点为0),那么level的大小就代表了经过了多少经销商,需要涨价多少次,用层序遍历访问所有结点,然后对每个结点的level做相应的更改,在遍历到叶节点时,根据货物数量进行计算,加到最终的答案ans中。第二种是建树+DFS,一次性遍历到整条路径的底部(零售商),然后计算这个叶节点的结果加入到ans中,再去遍历其他路径,依次计算出结果,递归的边界条件是叶节点

DFS请戳这里:DFS_study

 

AC代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<algorithm> 
#include<cmath>
using namespace std;
const int maxn=100010;
int maxlevel=0,num=0;
double ans=0; 
struct node{
	double weight;
	vector<int> child;
}Tree[maxn];
double price,rate;
void DFS(int index,int level);
int main()
{
	int n;
	int cnt;
	cin>>n>>price>>rate;
	rate/=100;
	for(int i=0;i<n;i++)
	{
		cin>>cnt;
		if(cnt==0)
		{
			int weight;
			cin>>weight;
			Tree[i].weight=weight;
		}
		else
		{
			for(int j=0;j<cnt;j++)
			{
				int temp;
				cin>>temp;
				Tree[i].child.push_back(temp);
			}
		}
	}
	DFS(0,0);
	printf("%.1f",price*ans);
	return 0;
}

void DFS(int index,int level)
{
	if(Tree[index].child.size()==0)//找到叶节点 
	{
		ans+=Tree[index].weight*pow(1+rate,level);//计算出货物总量,用重量增加代替价格增加 
		return;
	}
	for(int i=0;i<Tree[index].child.size();i++)//递归的访问每一个子节点 
		DFS(Tree[index].child[i],level+1);
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值