完全背包与01背包不同之处在于,每件物品的数量都是无限的。

在处理的时候:01背包是后面的前面dp, 完全背包是从前面到后面dp

注意:输出语句的后面是有一个点的

#include <iostream>


using namespace std;

int n,v,c[10100],w[10100],f[10111];
int t;
int pigw,totalw;

const int M = 100000000;
int min(int a , int b){
	return (a > b)?b:a;
}
int main(){

	cin >> t;

	while(t--){

		cin>>pigw>>totalw;

		int maxw = totalw - pigw;

		cin >> n;

		for(int i = 1 ; i <= n ; ++i ){
			cin>>w[i]>>c[i];
		}

//		memset(f,100000000,sizeof(f));

		for(int i = 0 ; i <= maxw ; ++i){
			f[i] = M;
		}
//
//		cout<<"----------------------------------"<<endl;
//
//				for(int i = 1 ; i <= maxw ; ++i){
//					cout<<"f["<<i<<"]="<<f[i]<<endl;
//				}
//
//				cout<<"----------------------------------"<<endl;

		f[0] = 0;


		for(int i =1 ; i <=n ; ++i){
			for(int j = c[i]; j <= maxw ; ++j){
				f[j] = min(f[j],f[j - c[i]] + w[i]);
			}
		}

//		cout<<"----------------------------------"<<endl;
//
//		for(int i = 1 ; i <= maxw ; ++i){
//			cout<<"f["<<i<<"]="<<f[i]<<endl;
//		}
//
//		for(int i = 1 ; i <= n ; ++i){
//					cout<<"c["<<i<<"]="<<c[i]<<endl;
//				}
//
//		for(int i = 1 ; i <= n ; ++i){
//					cout<<"w["<<i<<"]="<<w[i]<<endl;
//				}
//
//
//		cout<<"----------------------------------"<<endl;

		if(f[maxw] == M){
			cout<<"This is impossible."<<endl;
		}else{
			cout<<"The minimum amount of money in the piggy-bank is "<<f[maxw]<<"."<<endl;
		}


	}
}