裸完全背包,注意把[1,F-E]初始化为INF。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <malloc.h>
typedef long long LL;
#define INF 500000001
using namespace std;
int dp[10001];
int main()
{
//freopen("test0.in", "r", stdin);
//freopen("test0.out", "w", stdout);
int T, E, F, N, p[1000], w[1000];
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &E, &F);
F -= E;
scanf("%d", &N);
dp[0] = 0;
for(int i = 1; i <= F; i++)
{
dp[i] = INF;
}
for(int i = 0; i < N; i++)
{
scanf("%d %d", &p[i], &w[i]);
for(int V = w[i]; V <= F; V++)
{
dp[V] = min(dp[V], dp[V - w[i]]+p[i]);
}
}
if(dp[F] != INF)
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[F]);
else
printf("This is impossible.\n");
}
return 0;
}