Dhu 1114 piggy-Bank (full backpack, variant)

Source: Internet
Author: User
Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1114====================================, Give you the weight of the empty piggy bank, the weight of the piggy bank, and the value and weight of the coin, let you estimate the minimum value of money in the piggy bank. because the number of coins is not 1, we need to analyze the dynamic equation and require the minimum value when we consider using a full backpack, well understood, using the min function enables DP to store the minimum value. In this case, we need to initialize the DP array Maximum ValueAnd DP [0] = 0, Indicates that nothing can be put in. Then we will consider how to update the DP array. DP retains the minimum value, which indicates the weight problem. Therefore, the dynamic equation can be listed as follows: DP [I] = min (DP [I], DP [I-weight] + value). The AC code is as follows:
 
#include<iostream>#include<stdio.h>using namespace std;#define INF 100000000int dp[50010];int value[50010],weight[50010];int V,num;int E,F;int min(int a,int b){if(a>b)return b;elsereturn a;}void CompletePack(int value,int weight){int i;for(i=weight;i<=V;i++)dp[i]=min(dp[i],dp[i-weight]+value);}int main(){int Case;scanf("%d",&Case);while(Case--){int i;scanf("%d %d",&E,&F);scanf("%d",&num);V=F-E;for(i=1;i<=num;i++)scanf("%d %d",&value[i],&weight[i]);for(i=1;i<=V;i++)dp[i]=INF;dp[0]=0;for(i=1;i<=num;i++)CompletePack(value[i],weight[i]);if(dp[V]>=INF)printf("This is impossible.\n");elseprintf("The minimum amount of money in the piggy-bank is %d.\n",dp[V]);}return 0;}

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.