PAT Grade --1090 Highest Price in Supply Chain (BFS or DFS)

This paper released simultaneously in CSDN: https://blog.csdn.net/weixin_44385565/article/details/94325835

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 (≤), the total number of the members in the supply chain (and hence they are numbered from 0 to N1); 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 −. 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 1.

Sample Input:

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output:

1.85 2

Topic effect: to find out the number of the highest selling price at a retailer's supply chain and find the price. Starting source suppliers root, each one more middleman, will increase r%, consumers purchase goods from retailers.

Ideas: methods varied, this problem is no different with 1106, with the shortest graph theory (the longest) path can do, regard it as a tree (there is no closed loop directed graph), with DFS or BFS can be. Here we regard it as a tree,

A, BFS record depth, then you can find the number of leaf nodes that the deepest layer of.

. 1 #include <the iostream>
 2 #include <Vector>
 . 3 #include <the cmath>
 . 4 #include <Queue>
 . 5  the using  namespace STD;
 . 6  
. 7  struct Node {
 . 8      int Level = 0 ;
 . 9      Vector < int > Child;
 10  };
 . 11  
12 is Vector <node> catena alberghiera;
 13 is Vector < int > retailer; // number (retailer) records each of the leaf nodes 
14  
15  int getDepth ( int & the root);
16 
17 int main()
18 {
19     int N, root = 0;
20     double P, r;
21     scanf("%d%lf%lf", &N, &P, &r);
22     chain.resize(N);
23     retailer.resize(N, 0);
24     for (int i = 0; i < N; i++) {
25         int index;
26         scanf("%d", &index);
27         if (index != -1) {
28             chain[index].child.push_back(i);
29         }
30         else {
31             root = i;
32         }
33     }
34     int depth = getDepth(root);
35     printf("%.2lf %d\n", P * pow(1 + r / 100, depth), retailer[depth]);
36     return 0;
37 }
38 
39 int getDepth(int& root) {
40     int index = root,
41         depth = 0;
42     queue<int> Q;
43     Q.push(index);
44     while (!Q.empty()) {
45         index = Q.front();
46         Q.pop();
47         if (chain[index].child.size() == 0)
48             retailer[chain[index].level]++;
49         if (depth < chain[index].level)
50             depth = chain[index].level;
51         int childIndex;
52         for (int i = 0; i < chain[index].child.size(); i++) {
53             childIndex = chain[index].child[i];
54             chain[childIndex].level = chain[index].level + 1;
55             Q.push(childIndex);
56         }
57     }
58     return depth;
59 }

 Two, the DFS seeking depth, while the number of recording each leaf node

 1 #include <iostream>
 2 #include <vector>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 int depth = 0;
 7 vector<vector<int> > chain;
 8 vector<int> retailer;
 9 
10 void DFS(int index, int level);
11 
12 int main()
13 {
14     int N, root = 0;
15     double P, r;
16     scanf("%d%lf%lf", &N, &P, &r);
17     chain.resize(N);
18     retailer.resize(N, 0);
19     for (int i = 0; i < N; i++) {
20         int index;
21         scanf("%d", &index);
22         if (index != -1)
23             chain[index].push_back(i);
24         else
25             root = i;
26     }
27     DFS(root, 0);
28     printf("%.2lf %d\n", P * pow(1 + r / 100.0, depth), retailer[depth]);
29     return 0;
30 }
31 
32 void DFS(int index, int level) {
33     if (depth < level)
34         depth = level;
35     if (chain[index].size() == 0)
36         retailer[level]++;
37     for (int i = 0; i < chain[index].size(); i++) {
38         DFS(chain[index][i], level + 1);
39     }
40 }

 

Guess you like

Origin www.cnblogs.com/yinhao-ing/p/11110027.html