题意:给出每种货币的价值和重量,及储蓄罐里面货币的总重量,求货币价值的最小值
dp【i】表示重量为i的最小价值,初始化后跑一下完全背包即可
const int N = 10005;
int dp[N], val[N], cost[N];
int n;
int main()
{
int tot;
for( scanf("%d", &tot); tot--; ) {
int e, r, m;
scanf("%d%d", &e, &r);
m = r - e;
scanf("%d", &n);
for( int i = 1; i <= n; ++i ) {
scanf("%d%d", &val[i], &cost[i]);
}
memset( dp, inf, sizeof( dp ) );
dp[0] = 0;
for( int i = 1; i <= n; ++i ) {
for( int j = 0; j <= m; ++j ) {
if( j >= cost[i] )
dp[j] = min( dp[j], dp[j-cost[i]] + val[i] );
}
}
if( dp[m] != inf )
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[m] );
else
puts("This is impossible.");
}
return 0;
}