码迷,mamicode.com
首页 > 其他好文 > 详细

PAT(Advanced Level)A1106. Lowest Price in Supply Chain

时间:2020-10-05 22:33:38      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:clu   oid   bfs   algorithm   lse   scan   ack   names   ret   

题意

供应链有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;
}

PAT(Advanced Level)A1106. Lowest Price in Supply Chain

标签:clu   oid   bfs   algorithm   lse   scan   ack   names   ret   

原文地址:https://www.cnblogs.com/MartinLwx/p/13770977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!