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

目錄

1,題目描述

 題目大意

2,思路

3,AC代碼

4,解題過程


1,題目描述

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搜索,記錄遍歷的層數即可:

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,解題過程

一發入魂

 

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