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;}