本文主要是介绍【PAT】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.
翻译:一个供货链是由一张零售商和经销商和供应商组成的网络——从供应商到客户之间每个人都紧紧相连。
从一个总供应商开始,每个在链上的人从各自的供应商上以P的价格买产品并以r%的利润卖给或批给其他人。只有零售商将会直面消费者。假设除了总供应商之外每个供应链上的成员都有一个供应商,并且没有供应环。
现在给你一个供应链,你需要说出所有零售商的总销售额。
INPUT FORMAT
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.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括三个正整数:N(<=10^5),供应链的总人数(因此他们的ID被标记为从0-N-1,并且总供应商的ID是0);P,代表总供应商的原价;r,代表每个经销商或零售商的利率。接着N行,每行根据以下格式描述一个经销商或零售商:
Ki ID[1] ID[2] … ID[Ki]
在第i行,Ki为从供应商i进货的经销商或零售商的总数,接着为这些经销商或零售商的ID号。Kj为0代表第j个人为一个零售商,作为替代Kj后给出的为Kj卖出的商品总数。一行内所有数字之间用空格隔开。
OUTPUT FORMAT
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.
翻译:对于每组输入数据,输出一行我们可以预测的所有零售商的总销售额,保留一位小数。数据保证数字不会大于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
解题思路
这道题就是深度优先搜索,每一次step+1,即多一次增税,当遇到retailer时,就将当前利率乘以其所卖商品数加到答案上即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
double P,r,sum=0.0;
vector<int> G[100010];
int retailer[100010];
void dfs(int root,double price){if(G[root].size()==0){sum+=price*retailer[root];return ;}for(int i=0;i<G[root].size();i++){dfs(G[root][i],price*r);}
}
int main(){scanf("%d%lf%lf",&N,&P,&r);r=1+r/100; int M,a; for(int i=0;i<N;i++){scanf("%d",&M);for(int j=0;j<M;j++){scanf("%d",&a);G[i].push_back(a);}if(M==0){scanf("%d",&a);retailer[i]=a;}}dfs(0,P);printf("%.1lf\n",sum);return 0;
}
这篇关于【PAT】1079. Total Sales of Supply Chain (25)【深度优先搜索】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!