UVA 10626 Buying Coke (DP memory Search)

Source: Internet
Author: User

UVA 10626 Buying Coke

I often buy Coca-Cola from the vending. 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 have inserted sufficient amount of money), I receive a Coke followed by the ex Change (if any). The exchange is always given on as few coins as possible (this is uniquely determined by the coin set used). This procedure was 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's the least number of coins I must insert, given the number of Cokes I want to buy and the number of coins I ha ve of each value? Please help me solve the problem while I create some harder problems for you. You may assume this machine won ' t run out of coins and that I are always having enough coins to buy all the Cokes I want.
Input

The first line is the input contains the number of test cases (at most 50). Each of the 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 ten, respectively). The input limits is 1 <= C <=, 0 <= N1 <=, 0 <= N5 <= and 0 <= N10 <= 50.
Output

For each test case, output a line containing a integer:the minimum number of coins needed to insert into the Vendi Ng Machine.
Sample Input

3

2 2 1 1

2 1 4 1

20 200 3 0

Output for Sample

5

3

148

The main topic: there are three kinds of coins 1 yuan N1, 5 yuan N5, 10 Yuan N10, to take to sell n bottles of 8 Yuan Cola, ask how coin will make the smallest number of coins invested. The idea of solving problems: The number of coins will be the smallest. Can be done with a memory search, VIS[N1][N5][N10] Mark whether the current remaining coin situation has occurred, if there has been a direct return DP[N1][N5][N10]
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#define MAX 1<<20using namespace STD;typedef Long Longllintvis[ the][ $][ -], dp[ the][ $][ -];intNum, N1, N5, N10;intDP (intNintN1,intN5,intN10) {if(Vis[n1][n5][n10]) {returnDP[N1][N5][N10]; }Else if(n = =0) {VIS[N1][N5][N10] =1; DP[N1][N5][N10] =0;returnDP[N1][N5][N10]; }Else{DP[N1][N5][N10] = MAX;if(N1 >=8) Dp[n1][n5][n10] = min (Dp[n1][n5][n10], DP (N-1, N1-8, N5, N10) +8);if(N5 >=1&& N1 >=3) Dp[n1][n5][n10] = min (Dp[n1][n5][n10], DP (N-1, N1-3, N5-1, N10) +4);if(N5 >=2) Dp[n1][n5][n10] = min (Dp[n1][n5][n10], DP (N-1, N1 +2, N5-2, N10) +2);if(N10 >=1) Dp[n1][n5][n10] = min (Dp[n1][n5][n10], DP (N-1, N1 +2, N5, N10-1) +1);if(N10 >=1&& N1 >=3) Dp[n1][n5][n10] = min (Dp[n1][n5][n10], DP (N-1, N1-3, N5 +1, N10-1) +4); VIS[N1][N5][N10] =1;returnDP[N1][N5][N10]; }}intMain () {intTscanf("%d", &t); while(t--) {scanf("%d %d%d%d", &num, &n1, &n5, &AMP;N10);memset(Vis,0,sizeof(VIS));printf("%d\n", DP (num, N1, N5, N10)); }return 0;}

UVA 10626 Buying Coke (DP memory Search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.