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;
    }
    
  • 相关阅读:
    Nginx 基本命令
    Nginx配置详细
    MySQL 函数大全
    X-Frame-Options 配置
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
    idea java 非web程序打包
    mysql 存储过程
    webstorm 重置所有设置
    vue input 赋值无效
    MySQL 性能优化神器 Explain 使用分析
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/13770977.html
Copyright © 2011-2022 走看看