PAT_A1106#Lowest Price in Supply Chain

Source:

PAT A1106 Lowest Price in Supply Chain (25 分)

Description:

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 Pand 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 (≤), the total number of the members in the supply chain (and hence their ID's are numbered from 0 to N1, 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 1.

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

Keys:

Attention:

  • 加上剪枝的话,层次遍历会更快一些,深度遍历比较好写-,-

Code:

 1 /*
 2 time: 2019-06-28 14:08:25
 3 problem: PAT_A1106#Lowest Price in Supply Chain
 4 AC: 24:26
 5 
 6 题目大意:
 7 供应链网络:供应商->经销商->零售商->消费者
 8 只有一个供应商,经销商以价格P从供应商进货,再以溢价r%出售给下一级经销商,最终至零售商出售给消费者
 9 求出消费者购买商品的最低价格
10 输入:
11 第一行给出,结点数N<=1e5(编号0~n-1,root为0),出厂价P,溢价百分率r
12 接下来N行,结点i的孩子结点数量,各孩子结点编号
13 输出:
14 最低价格(4位小数),零售商数量(叶子结点数量)
15 
16 基本思路:
17 实质就是遍历一个树,求叶子结点中最浅的层次和个数
18 */
19 #include<cstdio>
20 #include<vector>
21 #include<cmath>
22 using namespace std;
23 const int M=1e5+10;
24 vector<int> chain[M];
25 int cnt=0,minDeep=M;
26 double r,p;
27 
28 void Travel(int root, int hight)
29 {
30     if(minDeep < hight)
31         return;
32     if(chain[root].size() == 0)
33     {
34         if(hight < minDeep)
35         {
36             minDeep = hight;
37             cnt = 1;
38         }
39         else if(minDeep == hight)
40             cnt++;
41         return;
42     }
43     for(int i=0; i<chain[root].size(); i++)
44         Travel(chain[root][i], hight+1);
45 }
46 
47 int main()
48 {
49 #ifdef ONLINE_JUDGE
50 #else
51     freopen("Test.txt", "r", stdin);
52 #endif // ONLINE_JUDGE
53 
54     int n,k,v;
55     scanf("%d%lf%lf", &n,&p,&r);
56     for(int i=0; i<n; i++)
57     {
58         scanf("%d", &k);
59         for(int j=0; j<k; j++)
60         {
61             scanf("%d", &v);
62             chain[i].push_back(v);
63         }
64     }
65     Travel(0,0);
66     printf("%.4f %d", p*pow((1.0+r/100),minDeep), cnt);
67 
68 
69     return 0;
70 }

 

posted @ 2019-06-28 14:43  林東雨  阅读(297)  评论(0编辑  收藏  举报