#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
using namespace std;
typedef long long LL; //数据太大了记得用LL,还有把INF也得换
typedef pair<int, int> P;
const int maxn = 1e7+10; //cf数组最多差不多是这么多,1e8就会段错误
const int V_MAX = 800 + 10;
const int mod = 10007;
const int INF = 0x3f3f3f3f; //如果数据范围为long long,记得把INF的值换了
const double eps = 1e-6; //eps开太小二分容易死循环,所以直接来个100次循环就好了
//多组输入时,maxn太大,用memset()会超时,血的教训;
//完全背包
//dp[i][j]表示购买前i件物品,容量为j时总价值的最小值
//dp[i] = min(dp[i - W[i]] + W[i], dp[i]);
LL W[1000], V[1000], dp[20000];
void solve() {
LL w, E, F;
cin >> E >> F;
w = F - E;
int n; cin >> n;
for(int i = 0; i < n; i++) cin >> V[i] >> W[i];
for(int i = 0; i <= w; i++) dp[i] = INF;
dp[0] = 0;
for(int i = 0; i < n; i++) {
for(int j = W[i]; j <= w; j++) {
dp[j] = min(dp[j - W[i]] + V[i], dp[j]);
}
}
if(dp[w] == INF) cout << "This is impossible.\n";
else cout << "The minimum amount of money in the piggy-bank is " << dp[w] << ".\n";
}
int main()
{
ios::sync_with_stdio(false); //关了流同步就别用c的输入
//freopen("D:\\in.txt", "r", stdin);
int t; cin >> t;
while(t--) {
solve();
}
return 0;
}
HDU - 1114 Piggy-Bank
最新推荐文章于 2022-05-02 20:08:36 发布