提交时间:2024-05-14 16:21:00
运行 ID: 146826
#include<bits/stdc++.h> using namespace std; int p[505],w[505],dp[505][10005]; void slove(){ int e,f,n; cin>>e>>f>>n; f -= e; for (int i = 1;i <= n;i++)cin>>p[i]>>w[i]; memset(dp,0x3f3f3f3f,sizeof(dp)); dp[0][0] = 0; for (int i = 1;i <= n;i++)for (int j = 0;j <= f;j++)for (int k = 0;k * w[i] <= j;k++)dp[i][j] = min(dp[i][j],dp[i - 1][j - k * w[i]] + k * p[i]); if (dp[n][f] >= 0x3f3f3f3f)cout<<"This is impossible.\n"; else cout<<"The minimum amount of money in the piggy-bank is "<<dp[n][f]<<".\n"; return ; } int main(){ int t; cin>>t; while (t--)slove(); return 0; }