hdu -1114(完全背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114
思路:求出存钱罐装全部装满情况下硬币的最小数量,即求出硬币的最小价值。转换为最小背包的问题。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int INF = 500500; int dp[INF],val[INF],we[INF]; int main(void) { int t,n,i,j,m,st,ed,tp; cin>>t; while(t--) { cin>>st>>ed; cin>>n; tp=ed-st; for(i=0;i<n;i++) cin>>val[i]>>we[i]; memset(dp,INF,sizeof(dp)); dp[0]=0; for(i=0;i<n;i++) { for(j=we[i];j<=tp;j++) { dp[j]=min(dp[j],dp[j-we[i]]+val[i]); } } //cout<<"--"<<num[tp]<<endl; if(dp[tp]!=dp[500100]) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[tp]); else printf("This is impossible.\n"); } return 0; }