Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8840 | Accepted: 4305 |
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.
題意:給出儲蓄罐狀態分別爲空和滿時的重量,以及不同面值的硬幣對應的質量。要求出儲蓄罐中至少有多少錢。
/*完全揹包*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define maxn 100000005
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int t,e,f,weight,n,p[600],w[600],dp[10005];
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&e,&f);
weight=f-e;
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%d%d",&p[i],&w[i]);
for(int i = 0; i < 10005; i++)
dp[i] = maxn;
dp[0]=0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= weight; j++)//完全揹包,從0到weight循環;01揹包反之。
{
if(j >= w[i])
dp[j]=min(dp[j],dp[j-w[i]]+p[i]);
}
}
if(dp[weight] == maxn)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[weight]);
}
}