您的位置:首页 > 大数据 > 人工智能

PAT (Advanced Level)-1106 Lowest Price in Supply Chain

2018-10-14 16:46 323 查看

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:

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

题目大意:给予root,结点 求价格链中的最小价格 而子结点会随着树的深度增加1 价格增加r% 因此求出树的最小深度 在该深度下 数一下叶结点数量 

思路 dfs 根结点0,深度0 开始 遇到叶结点进行判断

[code]#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

int num,mindepth=999999;
vector<int>v[100002];
void dfs(int idx,int depth)
{
if(depth>mindepth)return;
if(v[idx].size()==0)
{
if(depth==mindepth)num++;
else if(depth<mindepth)
{
num=1;
mindepth=depth;
}
}
for(int i=0;i<v[idx].size();i++)
{
dfs(v[idx][i],depth+1);
}
}
int main()
{
int n;
cin>>n;
double p,r;
cin>>p>>r;
for(int i=0;i<n;i++)
{
int k;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
int t;
scanf("%d",&t);
v[i].push_back(t);
}
}
dfs(0,0);
printf("%.4lf %d\n",p*pow(1+r/100,mindepth),num);
}

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: