首页 > 代码库 > HDU-1114-Piggy-Bank
HDU-1114-Piggy-Bank
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1114
完全背包
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int MAXN=999999999;
int main(void)
{
int i,j,k,l;
int n,m,t;
int e,f;
int w[505],v[505];
int dp[10005];
scanf("%d",&t);
while(t--)
{
//memset(dp,0,sizeof(dp));
scanf("%d%d",&e,&f);
int s=f-e;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",v+i,w+i);
}
for(i=1;i<=s;i++)
dp[i]=MAXN;
dp[0]=0;
for(i=0;i<n;i++)
{
for(j=w[i];j<=s;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[s]==MAXN||s<=0)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[s]);
}
return 0;
}
参数
62MS | 340K | 884 B |
HDU-1114-Piggy-Bank