提交时间:2024-05-06 22:10:15
运行 ID: 146020
#include <bits/stdc++.h> using namespace std; struct str { int w, v; double wv; bool operator < (const str &o) const { return wv<o.wv; } } s[501]; int n; bool b = true; void a1(int now, int x, int sum) { if(b==false) { return; } if(x==n+1) { if(now==0) { cout << "The minimum amount of money in the piggy-bank is " << sum << "." << endl; b=false; } return; } for(int i = now/s[x].w; i>=0; i--) { a1(now-s[x].w*i, x+1, sum+s[x].v*i); } } int main() { int t; cin >> t; for(int i = 1; i<=t; i++) { int w1, w2; cin >> w1 >> w2; int w = w2-w1; cin >> n; for(int j = 1; j<=n; j++) { cin >> s[j].v >> s[j].w; s[j].wv=s[j].v*1.0/s[j].w; } sort(s+1, s+n+1); a1(w, 1, 0); if(b) { cout << "This is impossible." << endl; } b=true; } return 0; }