Piggy-Bank
http://acm.hdu.edu.cn/showproblem.php?pid=1114
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3183 Accepted Submission(s): 1577
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!
3 10 110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4
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.
//这是完全背包,bag[j]表示存钱罐的容积为j时存钱的最小值
#include<iostream>
#include<cstring>
using namespace std;
const int INF=999999999;
int bag[10005],value[505],volume[505];
int main(){
int t,V,N,e,f;
scanf("%d",&t);
while(t--){
scanf("%d%d",&e,&f);
V=f-e;
for(int i=0;i<=V;i++) //这题是求最小值,所以初始化为最大,若是求最大值则初始化为最小
bag[i]=INF;
bag[0]=0; //这步不要忘记哦
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%d%d",&value[i],&volume[i]);
for(int i=1;i<=N;i++)
for(int v=volume[i];v<=V;v++) //这里和01背包顺序是相反的
bag[v]=min(bag[v],bag[v-volume[i]]+value[i]);
if(bag[V]==INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",bag[V]);
}
//system("pause");
return 0;
}