UVA 10626--buying coke+ Memory Search +DP

Source: Internet
Author: User

Topic Link: Click to enter
The original definition state Dp[n][n1][n5][n10] means the number of coins to be spent on the remaining 1,5,10 coins n1,n5,n10 when buying n bottles of Coke. Then the state transfer is: 1.8 One cent coins purchase of the nth bottle Cola, t=dp[n-1][n1+8][n5][n10]+8; 2. A five-point and 3-1-point, t=dp[n-1][n1+3][n5+1][n10]+4; 3. Two of 5 t=dp[n-1][n1][n5+2][n10]; 4. A 10-point, t=dp[n-1][n5][n10+1].
For the 12th type of dp[n][n1][n5][n10]=min (T,dp[n][n1][n5][n10])
For 3, 4 kinds: dp[n][n1+2][n5][n10]=min (dp[n][n1+2][n10],t).
Then hand over the time-out, after looking at the other people's ideas, send themselves less consider a situation: Cast 3 1 points and a 10, get back a 5 points. Then you can write it with a memory search. Define status Dp[n1][n5][n10] indicates the minimum number of coins required to buy a bottle of Coke under the status (N1,N5,N10).

The code is as follows:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;#define INF 0x3f3f3f3fintdp[780][ the][ -];intSolveintNintN1,intN5,intN10) {if(dp[n1][n5][n10]!=-1)returnDP[N1][N5][N10];if(n==0)return 0;intMin=inf;if(n1>=8)    {intT=solve (n1, n1-8, N5,N10) +8;    Min=min (min,t); }if(n1>=3&&n5>=1)    {intT=solve (n1, n1-3, n5-1, N10) +4;    Min=min (min,t); }/ * if (n1>=3&&n10>=1) {int t=solve (n-1,n1-3,n5+1,n10-1) +4;    Min=min (t,min); }*/    if(n5>=2)    {intT=solve (n1, n1+2, n5-2, N10) +2;    Min=min (t,min); }if(n10>=1)    {intT=solve (n1, n1+2, n5,n10-1)+1;    Min=min (min,t); }returnDp[n1][n5][n10]=min;}intMain () {//Freopen ("In.txt", "R", stdin);      intT,N,N1,N5,N10;scanf("%d", &t); while(t--) {scanf("%d%d%d%d", &AMP;N,&AMP;N1,&AMP;N5,&AMP;N10);memset(dp,-1,sizeof(DP));intAns=solve (N,N1,N5,N10);printf("%d\n", ans); }return 0;}

UVA 10626--buying coke+ Memory Search +DP

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.