PAT A1079 Total Sales of Supply Chain (25分)

本文介绍了一个供应链中总销售额的计算方法,供应链由供应商、经销商和零售商组成,从供应商开始,每级经销商都会增加一定比例的价格,最终计算所有零售商的总销售额。

PAT A1079 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

大体题意

根结点是供应商,给予一个基本价格P,非叶结点都是经销商,每经过一个结点多r百分比的价格,叶结点就是零售商,他的利润也是百分比r,并且也给了每个零售商的销售量
最后要求输出总的销售金额

思路

就是一个深度遍历的应用
每次经过一个结点就多一些价格,最后将每个叶结点的销售金额累加即可
设置结点结构体,其中只有叶结点的neednum才有效,表示销售量

typedef struct node
{
	int neednum;
	vector<int> children;
}node;

注意小数的取整问题

代码

#include <iostream>
using namespace std;
#include <vector>
#define maxn 100010

typedef struct node
{
	int neednum;
	vector<int> children;
}node;

int n;
double price, r;
node tree[maxn];
double total = 0;//记录总销售额

void dfs(int root, double price)
{
	double chainnum, nowprice;
	if (tree[root].children.size() == 0)//叶节点
	{
		chainnum = tree[root].neednum * price;
		total += chainnum;
	}
	else//非叶节点
	{
		for (int i = 0; i < tree[root].children.size(); i++)//对每个孩子进行深度遍历
		{
			nowprice = price * (1.0 + r / 100);//单价提高了r%
			dfs(tree[root].children[i], nowprice);//递归
		}
	}
}

int main()
{
	cin >> n >> price >> r;
	int k, need, x;//孩子数
	for (int i = 0; i < n; i++)
	{
		cin >> k;
		if (k == 0)//如果孩子数为0,即为叶节点,那么要记录销售量
		{
			cin >> need;
			tree[i].neednum = need;
		}
		else
		{
			for (int j = 0; j < k; j++)//如果是非叶节点,那么把它的孩子信息保存在children中
			{
				cin >> x;
				tree[i].children.push_back(x);
			}
		}
	}
	//输入完毕,通过深度遍历计算总销售额
	dfs(0, price);
	printf("%.1f\n", total);
	return 0;
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值