Problem Description
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.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 505;
int const INF = 0x3fffffff;
int w[MAX], p[MAX];
int dp[10005];
int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
int e, f;
scanf("%d %d", &e, &f);
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d %d", &p[i], &w[i]);
for(int i = 1; i <= f - e; i++)
dp[i] = INF;
dp[0] = 0;
for(int i = 1; i <= n; i++)
for(int j = w[i]; j <= f - e; j++)
dp[j] = min(dp[j], dp[j - w[i]] + p[i]);
if(dp[f - e] == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[f - e]);
}
}原文地址:http://blog.csdn.net/tc_to_top/article/details/46384035