poj 1726

http://poj.org/problem?id=1276

解题要点:用完全背包来模拟的解题,只不过加了限制条件used[]。。。其他的就一样了。。

注意: cash 和n 为0 的情况

 1 #include <iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int cash,n;
 5 int amount[20],w[20];
 6 int f[100006];
 7 int used[100006];
 8 int maxm(){
 9     if(cash==0||n==0){
10         return 0;
11     }
12     for(int i=1;i<=n;i++){
13         memset(used,0,sizeof(used));
14         for(int j=w[i];j<=cash;j++){
15             if((f[j]<f[j-w[i]]+w[i])&&(used[j-w[i]]+1<=amount[i])){
16                 f[j] = f[j-w[i]]+w[i];
17                 used[j] = used[j-w[i]]+1;
18             }
19         }
20     }
21     return f[cash];
22 }
23 int main()
24 {
25    while(cin>>cash>>n){
26         for(int i=1;i<=n;i++)
27             cin>>amount[i]>>w[i];
28         memset(f,0,sizeof(f));
29         cout<<maxm()<<endl;
30    }
31     return 0;
32 }
原文地址:https://www.cnblogs.com/Bang-cansee/p/3236113.html