PAT甲级 供应商问题 1079 1090 1106

供应商问题 (dfs)

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 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 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%,也就是变为上一层的(1 + r/100)倍,在叶子节点(零售商)给出货物数量,求价值之和。
Solution
建立起子节点的关系,dfs即可。
代码

#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long ll;

const int SZ = 1e5 + 10;
const int INF = 0x3f3f3f3f;

int n,k,x,dep;
double r,p,ans;

vector<int>node[SZ],amount(SZ+2);
inline void dfs(int x,int dept)
{
	if(!node[x].size())
	{
		ans += pow(1 + r/100.0,dept) * p * amount[x];
	}
	else 
	for(int i = 0;i < node[x].size();i ++ )
	{
		dfs(node[x][i],dept + 1);
	}
}

int main()
{
	scanf("%d%lf%lf",&n,&p,&r);
	for(int i = 0;i < n;i ++ )
	{
		scanf("%d",&k);
		if(k != 0)
		for(int j = 1;j <= k;j ++ )
		{
			scanf("%d",&x);
			node[i].push_back(x);
		}
		else 
		{
			scanf("%d",&x);
			amount[i] = x;
		}
	}
	dfs(0,0);
	printf("%.1lf",ans);
	//system("pause");
	return 0;
}

1090 Highest Price in 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. 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 highest price we can expect from some 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 they are numbered from 0 to N−1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number
S​i is the index of the supplier for the i-th member. S​root for the root supplier is defined to be −1. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 10^10.
Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2

题目大意
与上一题类似,求各个零售商(叶子结点)的最大价格,和有最大价格的零售商的数量。
Solution
dfs,记录最大深度的到达最大深度的次数。
代码

#include
#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long ll;

const int SZ = 1e5 + 7;
const int INF = 0x3f3f3f3f;

int n,root,dep = -1,num,x;
double p,r;

vector<int> node[SZ];

inline void dfs(int x,int dept)
{
    if(!node[x].size())
    {
        if(dept == dep) num ++ ;
        else if(dept > dep)
        {
            num = 1;
            dep = dept;
        }
    }
    else 
    for(int i = 0;i < node[x].size();i ++ )
    {
        dfs(node[x][i],dept + 1);
    }
    return;
}

int main()
{
    scanf("%d%lf%lf",&n,&p,&r);
    for(int i = 0;i < n;i ++ )
    {
        scanf("%d",&x);
        if( x != -1)
        node[x].push_back(i);
        else root = i;
    }
    num = 1;
    dfs(root,0);
    printf("%.2lf %d",pow(1 + r/100.0,dep) * p,num);
    //system("pause");
    return 0;
}

1106 Lowest Price in 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 lowest price a customer can expect from some 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 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. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 10^10.
Sample Input:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
0
0
0
Sample Output:
1.8362 2
题目大意
与上题类似,求零售商(叶子结点)的最小价格,和有最小价格的零售商的数量。
Solution
记录叶子结点的最小深度,和能到最小深度的叶子节点的数量。
代码

#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long ll;

const int SZ = 1e5 + 10;
const int INF = 0x3f3f3f3f;

int n,k,x,num,dep = INF;
double p,r;
vector<int> node[SZ];

inline void dfs(int x,int dept)
{
    if(node[x].size() == 0)
    {
        if(dept < dep) 
        {
            dep = dept;
            num = 1;
        }
        else if(dept == dep) num ++ ;
    }
    else 
    for(int i = 0;i < node[x].size();i ++ )
    {
        dfs(node[x][i],dept + 1);
    }
}

int main()
{
    scanf("%d%lf%lf",&n,&p,&r);
    for(int i = 0;i < n;i ++ )
    {
        scanf("%d",&k);
        for(int j = 1;j <= k;j ++ )
        {
            scanf("%d",&x);
            node[i].push_back(x);
        }
    }
    num = 1;
    dfs(0,0);
    printf("%.4lf %d",p * pow(1 + r/100,dep),num);
    //system("pause");
    return 0; 
}

2020.3.7

你可能感兴趣的:(PAT,树结构,dfs)