PAT甲級 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 (<=105), 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:

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

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


題目大意

供應商只有一個(對應樹的根),只有零售商能直接面對顧客(對應樹的葉子),經銷商就是在供應商和零售商之間的商家(對應樹的非葉、非根結點)。

一個商家只有一個提供商品的商家(除根外),並且沒有商品循環。(原文中的這句話沒什麼用,說明了是一棵樹)。

輸入n個商家(供應商或零售商或經銷商),和初始價格p,以及每經過一層的增長率r(比上一級增長了r%)。

接下來輸入n各商家的信息,k表示有k個商家從當前商家處進貨,k=0表示是零售商。

問你顧客能得到的最低價格,以及按最低價格賣出商品的零售商(k=0的)。

樣例解析:(0)是供應商,(4、7、9、8)是零售商,每經過一級商品價格變爲price*(1+r%)。

因此要求的零售商就是4,和7,商品價格就是p*(1+r%)*(1+r%)。

解題思路

輸入的信息:“提供商品的商家”和“接受該商家商品的商家”之間建邊,深搜求每個結點的深度,求出最小深度的葉子節點深度及數量就可以了。

#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
const int MAXN = 100000 + 5;
const int MAXM = 100000 + 5;
const int INF = 0x7f7f7f7f;
template <class XSD> inline XSD f_min(XSD a, XSD b) { if (a > b) a = b; return a; }
template <class XSD> inline XSD f_max(XSD a, XSD b) { if (a < b) a = b; return a; }
int n, m;
double p, r;
vector<int>mp[MAXN];
bool vis[MAXN], check[MAXN];
int deep[MAXN];
void Dfs(int root, int dee){
    deep[root] = dee;
    for(int i=0; i<mp[root].size(); i++){
        Dfs(mp[root][i], dee+1);
    }
}
void Getdata(){
    int k;
    memset(deep, 0, sizeof deep);
    memset(vis, false, sizeof vis);
    memset(check, false, sizeof check);
    for(int i=0; i<n; i++){
        scanf("%d", &k);
        if(!k) vis[i]=true;///零售商
        else{
            int x;
            for(int j=0; j<k; j++){
                scanf("%d", &x);
                check[x] = true;
                mp[i].push_back(x);
            }
        }
    }
}
void Solve(){
    int root;
    for(int i=0; i<n; i++) if(!check[i]){root=i;break;}
    Dfs(root, 0);
    int times=INF;
    for(int i=0; i<n; i++) if(vis[i]) times=f_min(times, deep[i]);
    int cnt=0;
    double rate=1.0+r/100.0;
    for(int i=0; i<times; i++) p*=rate;
    for(int i=0; i<n; i++) if(vis[i]&&deep[i]==times) cnt++;
    printf("%.4f %d\n", p, cnt);
}
int main(){
    while(~scanf("%d%lf%lf", &n, &p, &r)){
        Getdata();
        Solve();
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章