A1106 Lowest Price in Supply Chain (25 分| dfs| 树的遍历,附详细注释,逻辑分析)

写在前面

  • 思路分析
    • 提供1棵树,树根处货物价格为p,从根结点开始每往下1层,该层货物价格将会在父亲结点价格上增加r%。求叶子结点处能获得的最低价格及能提供最低价格的叶子结点数
    • 实现分析:
      • dfs保存深度最小值mindepth,及最小值下该深度叶子结点个数minnum
      • 深度优先搜索参数为index和depth,不断遍历index结点的孩子结点,直到当前结点没有孩子结点为止
  • 实现方案,学习ing

测试用例

  • 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
    output:
    1.8362 2
    

ac代码

  • 代码容易理解,不再赘述
    #include <cstdio>
    #include <vector>
    #include <cmath>
    using namespace std;
    vector<int> v[100005];
    int mindepth = 99999999, minnum = 1;
    
    void dfs(int inx, int depth)
    {
        if(mindepth < depth)
            return ;
        if(v[inx].size() == 0)
        {
            if(mindepth == depth)
                minnum++;
            else if(mindepth > depth)
            {
                mindepth = depth;
                minnum = 1;
            }
        }
        for(int i = 0; i < v[inx].size(); i++)
            dfs(v[inx][i], depth + 1);
    }
    
    int main()
    {
        int n, k, c;
        double p, r;
        scanf("%d %lf %lf", &n, &p, &r);
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &k);
            for(int j = 0; j < k; j++)
            {
                scanf("%d", &c);
                v[i].push_back(c);
            }
        }
    
        dfs(0, 0);
        printf("%.4f %d", p * pow(1 + r/100, mindepth), minnum);
        return 0;
    }
    
发布了328 篇原创文章 · 获赞 107 · 访问量 39万+

猜你喜欢

转载自blog.csdn.net/qq_24452475/article/details/100618992