Uva--10626(动规,记忆化搜索)

2014-08-21 10:50:10

Problem D
Buying Coke 
Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

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 15 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), n1n5n10 (the number of coins of value 15 and 10, respectively). The input limits are 1 <= C <= 1500 <= n1 <= 5000 <= 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                               Output for Sample Input

3 
2 2 1 1 
2 1 4 1 
20 200 3 0 
 
        
5 
3 
148 

  

 

 

思路:纯记忆话搜索,要注意的是找零有3种:(1)10元找 两张1元;(2)两张5元找 两张1元;(3)一张10元3张1元找 一张5元。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 const long long INF = 1e17;
 6 typedef long long ll;
 7 
 8 int Case;
 9 int n[3];
10 int v[3] = {1,5,10};
11 ll dp[705][155][55];
12 int c;
13 
14 ll Solve(int n,int v0,int v1,int v2){
15     if(n == 0)
16         return 0;
17     if(dp[v0][v1][v2] != -1)
18         return dp[v0][v1][v2];
19     ll tmin = INF;
20     if(v2){
21         tmin = min(tmin,Solve(n - 1,v0 + 2,v1,v2 - 1) + 1);
22     }
23     if(v1 && v0 >= 3){
24         tmin = min(tmin,Solve(n - 1,v0 - 3,v1 - 1,v2) + 4);
25     }
26     if(v1 >= 2){
27         tmin = min(tmin,Solve(n - 1,v0 + 2,v1 - 2,v2) + 2);
28     }
29     if(v0 >= 8){
30         tmin = min(tmin,Solve(n - 1,v0 - 8,v1,v2) + 8);
31     }
32     if(v0 >= 3 && v2){
33         tmin = min(tmin,Solve(n - 1,v0 - 3,v1 + 1,v2 - 1) + 4);
34     }
35     return dp[v0][v1][v2] = tmin;
36 }
37 
38 int main(){
39     scanf("%d",&Case);
40     while(Case--){
41         memset(dp,-1,sizeof(dp));
42         scanf("%d%d%d%d",&c,&v[0],&v[1],&v[2]);
43         printf("%lld\n",Solve(c,v[0],v[1],v[2]));
44     }
45     return 0;
46 }

 

posted @ 2014-08-21 10:51  Naturain  阅读(154)  评论(0编辑  收藏  举报