[dp+记忆化搜索]uva10626

31 篇文章 0 订阅

Description

I often buy Coca-Cola from the vending machine at work. Usually I buy several cokes at once, since my working mates also likes coke. A coke in the vending machine costs 8 Swedish crowns, and the machine accept crowns with the values 1, 5 and 10. As soon as I press the coke button (after having inserted sufficient amount of money), I receive a coke followed by the exchange (if any). The exchange is always given in as few coins as possible (this is uniquely determined by the coin set used). This procedure is repeated until I’ve bought all the cokes I want. Note that I can pick up the coin exchange and use those coins when buying further cokes.Now, what is the least number of coins I must insert, given the number of cokes I want to buy and the number of coins I have of each value? Please help me solve this problem while I create some harder problems for you. You may assume that the machine won’t run out of coins and that I always have enough coins to buy all the cokes I want.

Input

The first line in the input contains the number of test cases (at most 50).Each case is then given on a line by itself. A test case consists of four integers: C (the number of cokes I want to buy), n1,n5, n10 (the number of coins of value 1, 5 and 10, respectively). The input limits are 1 ≤ C ≤ 150,0 ≤n1≤ 500, 0 ≤ n5≤ 100 and 0 ≤ n10 ≤ 50.

Output

For each test case, output a line containing a single integer: the minimum number of coins needed to insert into the vending machine.

Sample Input

3
2 2 1 1
2 1 4 120 200 3 0

Sample Output

5
3
148

题目分析

       一个状态有4维:剩余要买的可乐数量lt,三种硬币各自的剩余量x、y、z。但由于初始硬币总值确定,所以lt可由x、y、z确定,只需要用三维表示一个状态,即可用a[i][j][k]表示面值为1、5、10的硬币分别剩余i、j、k个时,投入的硬币数量的最小值,然后记忆化搜索即可。
       依据题目,在一次购买行为中只能买一瓶。如果去掉这个限制,只需在尽可能少的情况下把足够的硬币放进贩卖机一次就可以买到所需数量的可乐,这是不同的问题。
       对于一次购买行为,有如下几种投硬币方案:

  • 投1个10分的 ,余2个1分
  • 投2个5分的,余2个1分
  • 投1个10分的+3个1分的,余1个5分
  • 投1个5分的+3个1分的
  • 投8个1分的
    注意购买可乐会找回硬币,所以开数组a时要在题目范围的基础上增加最大硬币数量

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int a[801][151][51];
int dfs(int,int,int,int);
int min(int,int);
const int inf=0x7f7f7f7f;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int c,x,y,z;
        cin>>c>>x>>y>>z;
        memset(a,0x7f,sizeof a);
        cout<<dfs(c,x,y,z)<<endl;
    }
    return 0;
}
int min(int a,int b)
{
    if(a<b) return a;
    return b;
}
int dfs(int lt,int x,int y,int z)
{
    int &cur=a[x][y][z];
    if(cur!=inf)
        return cur;
    if(lt==0)
    return cur=0;
    if(z>0)
        cur=min(cur,dfs(lt-1,x+2,y,z-1)+1);
    if(y>=2)
        cur=min(cur,dfs(lt-1,x+2,y-2,z)+2);
    if((z>0)&&(x>=3))
        cur=min(cur,dfs(lt-1,x-3,y+1,z-1)+4);
    if((y>0)&&(x>=3))
        cur=min(cur,dfs(lt-1,x-3,y-1,z)+4);
    if(x>=8)
        cur=min(cur,dfs(lt-1,x-8,y,z)+8);
    return cur;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值