PAT 1090.Highest Price in Supply Chain

1090. Highest Price in Supply Chain (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=105), 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 Si is the index of the supplier for the i-th member. Sroot 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 1010.

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2

题目解析:
从供应商开始每往下一层价格就提交r%,要求出最高价格,即最大供应链层数。
输入数组为每个客户的上一层供应商,若为-1则是源头供应商。采用邻接链表存储每个供应商的下层客户,对源头供应商依次进行深度优先遍历,找出最大层数,和最大层数的供应链条数。
若由每个客户查找源头供应商的层数,时间复杂度会超出。
 1 #include<stdio.h>
 2 #include<vector>
 3 
 4 using namespace std;
 5 
 6 class Trader{
 7 public:
 8     int id;
 9     vector<int> customer;
10 };
11 vector<Trader> list;
12 vector<int> root;
13 int maxlevel;
14 int num;
15 
16 void dfs(int start, int level){
17     if (list[start].customer.size() == 0){
18         if (level > maxlevel){
19             maxlevel = level;
20             num = 1;
21         }
22         else if (level == maxlevel){
23             num++;
24         }
25     }
26     else{
27         int i;
28         for (i = 0; i < list[start].customer.size(); i++){
29             dfs(list[start].customer[i], level + 1);
30         }
31     }
32 }
33 
34 int main(void){
35     int n;
36     double p, r;
37     scanf("%d %lf %lf", &n, &p, &r);
38     int i;
39     for (i = 0; i < n; i++){
40         Trader *trader = new Trader();
41         trader->id = i;
42         list.push_back(*trader);
43     }
44     int supply;
45     for (i = 0; i < n; i++){
46         scanf("%d", &supply);
47         if (supply != -1)
48             list[supply].customer.push_back(i);
49         else
50             root.push_back(i);
51     }
52     for (i = 0; i < root.size(); i++){
53         dfs(root[i], 0);
54     }
55     for (i = 0; i < maxlevel; i++){
56         p *= (r / 100 + 1);
57     }
58     printf("%.02lf %d\n", p, num);
59     return 0;
60 }

 

posted @ 2018-01-20 23:42  GrayWind  阅读(105)  评论(0)    收藏  举报