题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114
求一定容量下最小值
代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
#include<fstream>
#include<complex>
#include<algorithm>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
int v[10010],w[10010],dp[10010];
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
int e,f;
cin>>e>>f;
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>v[i]>>w[i];
}
for(int i=1;i<=f-e;i++) dp[i]=INF;
dp[0]=0;//记得初始化
for(int i=1;i<=n;i++){
for(int j=w[i];j<=f-e;j++){
dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[f-e]==INF) cout<<"This is impossible."<<endl;
else{
cout<<"The minimum amount of money in the piggy-bank is "<<dp[f-e]<<"."<<endl;
}
}
return 0;
}