题目描述
输入
输出
样例输入
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
样例输出
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.
分析: 完全背包。。
#include <iostream> #include <string> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <map> #define range(i,a,b) for(int i=a;i<=b;++i) #define LL long long #define rerange(i,a,b) for(int i=a;i>=b;--i) #define fill(arr,tmp) memset(arr,tmp,sizeof(arr)) using namespace std; int t,dp[10005]; int u[10005],v[10005]; void init() { cin>>t; } void solve(){ while(t--){ int x,y,n,tmp; cin>>x>>y; tmp=y-x; fill(dp,0x3f3f3f3f); dp[0]=0; cin>>n; range(i,1,n)cin>>u[i]>>v[i]; range(i,1,n)range(j,v[i],tmp)dp[j]=min(dp[j],dp[j-v[i]]+u[i]); if(dp[tmp]==0x3f3f3f3f)cout<<"This is impossible."<<endl; else cout<<"The minimum amount of money in the piggy-bank is "<<dp[tmp]<<"."<<endl; } } int main() { init(); solve(); return 0; }