#include<iostream>
#include<fstream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int MAX = 0x6fffffff;
int t,e,f,n,net, m;
struct coin{
int p;
int w;
};
int c[10010];
int main(){
// freopen("1.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&e,&f,&n);
net = f - e;
m = net + 1;
memset(c,0,sizeof(c));
for(int i = 0; i <= m; ++i){
c[i] = MAX;
}
coin * pig = new coin[n+1];
memset(pig,0,(n+1)*4);
for(int i = 1; i <= n; ++i){
scanf("%d%d",&pig[i].p,&pig[i].w);
}
for(int i = 1;i <= n; ++i){
c[0] = 0;
for(int j = pig[i].w; j <= net; ++j){
if(c[j-pig[i].w] + pig[i].p < c[j]){
c[j] = c[j-pig[i].w] + pig[i].p;
}
}
}
if(c[net] == MAX){
printf("This is impossible.\n");
}
else{
printf("The minimum amount of money in the piggy-bank is %d.\n",c[net]);
}
}
return 0;
}
Key:
每次循环时c[0]记得初始化~