zoukankan      html  css  js  c++  java
  • PAT(Advanced Level)A1106. Lowest Price in Supply Chain

    题意

    供应链有3种人,零售商,经销商和供应商,供应链上的人都可以从自己的供应商那里以P的价格买入,而后以r%的涨幅卖给下一级,问供应链上找零售商买价格最低是多少

    思路

    • 每一层的价格涨幅都是一样的,所以这个问题等价于从根结点出发找最短的路到零售商。用BFSDFS都可以做,DFS代码量少我就用DFS
    • 因此在到达递归边界的时候可以将其转化为递归深度的比较而不是最便宜价格的比较,可能执行起来会稍微快一点吧..

    代码

    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <vector>
    #include <queue>
    #include <math.h>
    using namespace std;
    int n;
    vector<int> tree[100010];
    double init_price, increment;
    double ans_price;
    int numbers = 0, ans_depth = 1000000000;
    void dfs(int cur, int depth)
    {
        if(tree[cur].size() == 0)
        {
            if(depth < ans_depth)
            {
                ans_depth = depth;
                numbers = 1;
            }else if(depth == ans_depth)
                numbers++;
        }
        for(int i=0;i<tree[cur].size();i++)
            dfs(tree[cur][i], depth + 1);
    }
    int main()
    {
        cin >> n >> init_price >> increment;
        increment /= 100;
        int t, tmp;
        for(int i=0;i<n;i++)
        {
            scanf("%d", &t);
            if(t == 0)
                continue;
            else{
                for(int j=0;j<t;j++)
                {
                    scanf("%d", &tmp);
                    tree[i].emplace_back(tmp);
                }
            }
        }
        dfs(0, 0);
        ans_price = init_price * pow(1 + increment, ans_depth);
        printf("%.4f %d", ans_price, numbers);
        return 0;
    }
    
  • 相关阅读:
    电话号码组合 hash表
    合并区间
    最小路径和 动态规划
    计数排序
    插入排序
    选择排序
    归并排序
    C#中不同程序集(dll)存在相同的命名空间
    生成otp token 脚本
    MySQL 组合索引、唯一组合索引的原理
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/13770977.html
Copyright © 2011-2022 走看看