如题:http://poj.org/problem?id=1384
Description
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
Input
Output
Sample Input
3 10 110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4
Sample Output
The minimum amount of money in the piggy-bank is 60. The minimum amount of money in the piggy-bank is 100. This is impossible.
往存钱罐里装钱 要求钱最多的同时重量满的时候钱要最少(注意是最少) f[j]=min(f[j],f[j-c[i]]+w[i]);
完全背包问题
唯一需要注意的是重量上限是满时的重量-空时的重量
f数组初始全部最大 f【0】=0重量0时钱是0
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define min(a,b)(a<b?a:b)
int E,F;
int c[505];
int w[505];
int f[10005];
int main()
{
// freopen("C:\\1.txt","r",stdin);
int T,n;
cin>>T;
while(T--)
{
scanf("%d%d",&E,&F);
int w1=F-E;
scanf("%d",&n);
int i,j;
for(i=1;i<=n;i++)
scanf("%d%d",&w[i],&c[i]);
for(i=0;i<10005;i++)
f[i]=0x0fffffff;
f[0]=0;
for(i=1;i<=n;i++)
for(j=c[i];j<=w1;j++)
f[j]=min(f[j],f[j-c[i]]+w[i]);
if(f[w1]==0x0fffffff)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",f[w1]);
}
}