1106 Lowest Price in Supply Chain (25 分)(PAT 甲級,樹的BFS,DFS)

A supply chain is a network of retailers(零售商), distributors(經(jīng)銷商), and suppliers(供應(yīng)商)-- 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 (≤10^5? ), 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[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 10^?10?? .

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

題目大意

供應(yīng)鏈由零銷商、經(jīng)銷商、供應(yīng)商組成,從供應(yīng)商開始,供應(yīng)鏈上的每個人在購買貨物的時候需要支付比原價格多r%的費用,供應(yīng)鏈上不存在環(huán),本題要求求出顧客為購買貨物支付的最少費用,并且求出最低價格的供應(yīng)鏈存在的條數(shù)

分析

本題涉及樹的遍歷,可以采用深度優(yōu)先遍歷或者廣度優(yōu)先遍歷,本題采用深度優(yōu)先遍歷,在遍歷的過程中記錄節(jié)點的層數(shù),當遇到葉子節(jié)點時,判斷當前的葉子節(jié)點是否符合題目要求,若層數(shù)level小于當前記錄的層數(shù)minlevel,則更新milevel并且設(shè)置count為1(當前符合路徑的數(shù)目),若層數(shù)level等于當前記錄的層數(shù)milevel,則說明有找到了一條符合題意的路徑,則自加1

 #include <iostream>
 #include <cmath>
 #include <vector>
 using namespace std;
 vector<vector<int> > v;
 vector<int> level;
 int count=1,minlevel=100010;
 void dfs(int u,int l){
    if(v[u].size()==0){
        level[u]=l;
        if(minlevel>l){
            count=1;
            minlevel=l;
         }else if(minlevel==l){
            count++;
         }
     }else{
        for(int w=0;w<v[u].size();w++){
            dfs(v[u][w],l+1);
         }
     }  
 }
 int main(){
    int n;
    double price,rate;
    cin>>n>>price>>rate;
    v.resize(n),level.resize(n);
    for(int i=0;i<n;i++){
        int cnt;
        cin>>cnt;
        v[i].resize(cnt);
        for(int j=0;j<cnt;j++){
            cin>>v[i][j];
         }
     }
     dfs(0,0);
     double ans=price*pow(1+rate/100.0,minlevel); 
     printf("%.4lf %d",ans,count);
    return 0;
 }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,104評論 0 23
  • Java應(yīng)用或者JavaEE Web應(yīng)用的性能是很重要的,尤其是數(shù)據(jù)庫后端對應(yīng)用的性能影響。不知你是否經(jīng)歷過Jav...
    菁華浮英夢閱讀 204評論 0 0
  • 我們像是活在一個最好的時代, 又好像是被壓在了一個最壞的時期。許許多多的人日復一日看似忙碌實則碌碌無為。
    榴蓮山閱讀 215評論 0 0
  • 1.dSYM你是如何分析的? 什么是 dSYM dSYM文件Xcode編譯項目后,我們會看到一個同名的 dSYM ...
    chilim閱讀 1,227評論 0 0

友情鏈接更多精彩內(nèi)容