树的遍历——A1106.Lowest Price in Supply Chain(25) 求树的深度最小的叶子结点 与A1190类似

Posted jasonpeng1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树的遍历——A1106.Lowest Price in Supply Chain(25) 求树的深度最小的叶子结点 与A1190类似相关的知识,希望对你有一定的参考价值。

技术图片

 

 

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int maxn = 100010;
vector<int> child[maxn];
double p,r;
int n,minDepth = 100,num = 0;
void DFS(int index,int depth){
    if(child[index].size() == 0){
        if(depth < minDepth){
            minDepth = depth;
            num = 1;
        }else if(depth == minDepth){
            num++;
        }
        return;
    }
    for(int i =0;i<child[index].size();++i){
        DFS(child[index][i],depth+1);
    }
}
int main(){
    int father,root;
    scanf("%d%lf%lf",&n,&p,&r);
    r /= 100;
    for(int i=0;i<n;++i){
        scanf("%d",&father);
        if(father != 0){
            //child[father].push_back(i);
            for(int j = 0;j<father;++j){
                int t;
                scanf("%d",&t);
                child[i].push_back(t);
            }
        }
    }
    DFS(0,0);
    printf("%.4f %d
",p * pow(1+r,minDepth),num);
    system("pause");
    return 0;
}

改进后:
技术图片

 

 

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int maxn = 100010;
const double INF = 1e12;
vector<int> child[maxn];
double p,r,ans = INF;
int n,minDepth = 100,num = 0;
void DFS(int index,int depth){
    if(child[index].size() == 0){
        double price = p * pow(1+r,depth);
        if(price < ans){
            ans = price;
            num = 1;
        }else if(price == ans){
            num++;
        }
        return;
    }
    for(int i =0;i<child[index].size();++i){
        DFS(child[index][i],depth+1);
    }
}
int main(){
    int father,root;
    scanf("%d%lf%lf",&n,&p,&r);
    r /= 100;
    for(int i=0;i<n;++i){
        scanf("%d",&father);
        if(father != 0){
            //child[father].push_back(i);
            for(int j = 0;j<father;++j){
                int t;
                scanf("%d",&t);
                child[i].push_back(t);
            }
        }
    }
    DFS(0,0);
    printf("%.4f %d
",ans,num);
    system("pause");
    return 0;
}

 

以上是关于树的遍历——A1106.Lowest Price in Supply Chain(25) 求树的深度最小的叶子结点 与A1190类似的主要内容,如果未能解决你的问题,请参考以下文章

1106. Lowest Price in Supply Chain (25)

1106. Lowest Price in Supply Chain (25)树+深搜——PAT (Advanced Level) Practise

1106. Lowest Price in Supply Chain (25)

1106 Lowest Price in Supply Chain (25 分)dfs

[建树(非二叉树)] 1106. Lowest Price in Supply Chain (25)

PAT甲级——A1106 Lowest Price in Supply Chain