[PATグレードAツリー/グラフのトラバーサル] 1106サプライチェーンの最低価格(25ポイント)

DFS

# include <bits/stdc++.h>
using namespace std;

int N;
double P, r;
vector< vector<int> > G;

double lowestPrice = 0xffffff;
int cnt = 0;
void DFS(int u, int lv){
    
    
    if(G[u].size() == 0) {
    
    
        double price = P * pow((1 + 0.01*r), lv);
        if(price < lowestPrice){
    
    
            lowestPrice = price;
            cnt = 1;
        } else 
        if(price == lowestPrice) {
    
    
            cnt++;
        }
        return;
    }
    for(int v: G[u]){
    
    
        DFS(v, lv + 1);
    }
}

int main(){
    
    
    cin >> N >> P >> r;
    G.resize(N);
    for(int u = 0;u < N;++u){
    
    
        int K;
        cin >> K;
        for(int j = 0;j < K;++j){
    
    
            int v;
            cin >> v;
            G[u].push_back(v);
        }
    }
    DFS(0, 0);
    printf("%.4lf %d\n", lowestPrice, cnt);
    
    return 0;
}

おすすめ

転載: blog.csdn.net/MYMarcoreus/article/details/113920194