很简单的背包入门题,如果不会背包的,可以留言,我把背包九讲发过去。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int p[505];
int w[505];
int dp[50005];
int main()
{
int a,b,test;
cin >> test;
while(test--)
{
cin>> a >>b;
b -= a;
int n;
cin >> n;
for(int i=1; i<=n; ++i)
cin>> p[i]>> w[i];
for(int i=0; i<= b; ++i)
dp[i] = 1000000;
dp[0] = 0;
for(int i=1; i<=n; ++i)
for(int j=w[i]; j<=b; ++j)
{
dp[j] = dp[j]>(dp[j-w[i]]+p[i])?(dp[j-w[i]]+p[i]):dp[j];
}
if(dp[b] == 1000000) cout <<"This is impossible."<<endl;
else
{
cout << "The minimum amount of money in the piggy-bank is "<<dp[b]<<"."<<endl;
}
}
return 0;
}