hdu 1114 Piggy-Bank 动态规划+完全背包

时间:2022-07-30 18:41:48
题意:给出空罐的质量w1和满罐的质量w2,给出n个硬币的价值和质量,求该质量下最小的价值。 完全背包,d[i]=min(d[i],d[i-w[j]]+v[j]),(i为质量,j为硬币编号)。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define INF 0x7ffffff
#define N 550
#define M 11000
using namespace std;

int w[N],v[N],d[M];

int main()
{
int T,w1,w2,w0,n;
cin>>T;
while(T--)
{
cin>>w1>>w2;
w0=w2-w1;
cin>>n;
for(int i=0;i<n;i++) cin>>v[i]>>w[i];
for(int i=1;i<=w0;i++) d[i]=INF;
d[0]=0;
for(int i=1;i<=w0;i++)
for(int j=0;j<n;j++)
if(i-w[j]>=0) d[i]=min(d[i],d[i-w[j]]+v[j]);
if(d[w0]>=INF) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n",d[w0]);
}
}