完全背包问题
让你尽可能的少装价值,装满背包
就把dp[0]=0即可,其他变成inf,然后就是标准的完全背包问题
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;
const int maxn = 1e6+5;
long long dp[maxn];
int w[maxn],v[maxn];
int main()
{
int i,j,t,a,b,c,n,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
scanf("%d",&n);
for(i = 1; i <= n; ++i)
scanf("%d%d",v+i,w+i);
c = b-a;
dp[0] = 0;
for(i = 1; i <= c; ++i)
dp[i] = 100000000;
for(i = 1; i <= n;++i)
{
for(j = w[i];j <= c; ++j)
{
dp[j] = min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[c]==100000000)
printf("This is impossible. ");
else
printf("The minimum amount of money in the piggy-bank is %lld. ",dp[c]);
}
}