1106 Lowest Price in Supply Chain

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 (≤), 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 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

题意:

  一件商品从supplier手中卖出去的价格是P,中间会经过若干个retailers 和 distributors每次商品都会在原来的价格基础上增加r%,而且客户只能够从retailers手中购买商品。求客户买到商品的最低价格,以及可以从多少个retailers手中以这个最低价格买入。

思路:

  因为题目说,除了supplier之外,其他的retailers和distributors只有一个供应商,所以可以用一个path[]来存储retailers或distributors的上一个供应商。最后遍历distributors通过path[]来找到最初的supplier,用minn来记录最小的路径长度。如果全都是这样遍历的话会有一组数据超时,所以我用了一个len[]来存储当前结点到supplier的路径长度,可以防止重复遍历,从而节省时间。但是最后还是有一组(第三组)测试点没有通过。目前还没找到原因。不过看别人的代码都是用DFS来做的,也可能是因为自己的思路和出题人的思路不一样所以才会被卡到吧。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int main() {
 6     int n;
 7     double p, r;
 8     scanf("%d %lf %lf", &n, &p, &r);
 9     vector<int> path(n, 0);
10     vector<int> len(n, 0);
11     vector<int> retailers;
12     len[0] = 1;
13     for (int i = 0; i < n; ++i) {
14         int k, t;
15         scanf("%d", &k);
16         if (k == 0)
17             retailers.push_back(i);
18         else
19             for (int j = 0; j < k; ++j) {
20                 scanf("%d", &t);
21                 path[t] = i;
22                 if (len[i] > 0) len[t] = len[i] + 1;
23             }
24     }
25     int minn = INT_MAX;
26     int ways = 1;
27     for (int i : retailers) {
28         int temp = i, count = 0, dummy;
29         while (path[temp] != 0) {
30             dummy = temp;
31             temp = path[temp];
32             if (len[temp] > 0) {
33                 count += len[temp];
34                 len[dummy] = len[temp] + 1;
35                 break;
36             } else
37                 count++;
38         }
39         len[i] = count;
40         if (len[i] < minn) {
41             ways = 1;
42             minn = len[i];
43         } else if (len[i] == minn) {
44             ways++;
45         }
46     }
47     double ans = p;
48     ans = pow(1 + r / 100.0, minn) * p;
49     printf("%.4lf %d
", ans, ways);
50     return 0;
51 }

2020-07-12 20:50:16

今天又把这道题给做了一下,感觉以前的自己好菜,本来是一道很简单的题目,当时竟然没想出来,今天有用DFS做了一下,测试用例全部通过。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int inf = 0x7fffffff;
 6 
 7 int len = inf;
 8 int num = 0;
 9 map<int, set<int> > m;
10 
11 void DFS(int p, int l) {
12     if (l > len) return ;
13     if (m[p].size() == 0) {
14         if (l < len) {
15             len = l;
16             num = 1;
17         } else if (l == len) {
18             num += 1;
19         }
20     }
21     for (auto it : m[p])
22         DFS(it, l+1);
23 }
24 
25 int main() {
26     int n;
27     double p, r;
28     cin >> n >> p >> r;
29     for (int i = 0; i < n; ++i) {
30         int k, t;
31         cin >> k;
32         for (int j = 0; j < k; ++j) {
33             cin >> t;
34             m[i].insert(t);
35         }
36     }
37     DFS(0, 0);
38     for (int i = 0; i < len; ++i) 
39         p *= (100 + r) / 100;
40     cout << fixed << setprecision(4) << p << " " << num << endl;
41     return 0;
42 }