完全背包求最少情况
初始化无穷大,dp[0]=0;
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXNUM 0X3f3f3f3f
int dp[50050],p[510],w[510];
int n;
int Min(int a,int b)
{
return a>b?b:a;
}
int main()
{
int T;
int i,j;
int e,f,wp;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&e,&f);
wp=f-e;
memset(p,0,sizeof(p));
memset(w,0,sizeof(w));
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i],&w[i]);
}
memset(dp,0x7f,sizeof(dp));
dp[0]=0;
for(i=0;i<n;i++)
{
for(j=w[i];j<=wp;j++)
{
dp[j]=Min(dp[j],dp[j-w[i]]+p[i]);
}
}
if(dp[wp]<MAXNUM) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[wp]);
else printf("This is impossible.\n");
}
return 0;
}