提交时间:2024-05-05 08:40:06

运行 ID: 145444

#include<bits/stdc++.h> using namespace std; const long long inf=5500000; signed main(){ int T,n,m,a[50005],b[50005],dp[50005];//第i项表示容量为i时最值 int e,f; cin>>T; for(int t=1;t<=T;t++){ memset(dp,inf,sizeof(dp)); dp[0]=0; cin>>m>>n; cin>>e; f=n-m; for(int i=1;i<=e;i++){ cin>>b[i]>>a[i]; } for(int i=1;i<=f;i++){ for(int j=1;j<=e;j++){ if(i-a[j]>=0) dp[i]=min(dp[i-a[j]]+b[j],dp[i]); } } if(dp[f]<inf) cout<<"The minimum amount of money in the piggy-bank is "<<dp[f]<<"."<<endl; else cout<<"This is impossible."<<endl; } }