天天看點

PAT_甲級_1090 Highest Price in Supply Chain (25point(s)) (C++)【DFS】

目錄

​​1,題目描述​​

​​ 題目大意​​

​​2,思路​​

​​3,AC代碼​​

​​4,解題過程​​

1,題目描述

PAT_甲級_1090 Highest Price in Supply Chain (25point(s)) (C++)【DFS】

Sample Input:

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

Sample Output:

1.85 2      

 題目大意

求一個供應鍊中,貨物可以賣出的最高價,以及賣出最高價的零售商數目

  • each number S​i​​ is the index of the supplier for the i-th member.:編号為0到N-1,Si即供應商,下标即商家的編号,比如3th=4,供應商4為商家3提供貨物;

2,思路

和這一題很像​​@&再見螢火蟲&【PAT_甲級_1079 Total Sales of Supply Chain (25point(s)) (C++)【DFS/scanf接受double】】​​

簡單的DFS搜尋,記錄周遊的層數即可:

PAT_甲級_1090 Highest Price in Supply Chain (25point(s)) (C++)【DFS】
PAT_甲級_1090 Highest Price in Supply Chain (25point(s)) (C++)【DFS】

3,AC代碼

#include<bits/stdc++.h>
using namespace std;
double P, r;
int N, maxDep = 0, num = 0;
vector<int> data[100003];
void dfs(int r, int dep){
    if(data[r].size() == 0){
        if(dep > maxDep){
            maxDep = dep;
            num = 1;
        }else if(dep == maxDep)
            num++;
    }
    for(int i = 0; i < data[r].size(); i++)
        dfs(data[r][i], dep + 1);
}
int main(){
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
    scanf("%d %lf %lf", &N, &P, &r);
    int id, root;
    for(int i = 0; i < N; i++){
        scanf("%d", &id);
        if(id == -1)
            root = i;
        else
            data[id].push_back(i);
    }
    dfs(root, 0);
    printf("%.2f %d", P * pow(1 + r / 100, maxDep), num);
    return 0;
}      

4,解題過程

繼續閱讀