题目链接:acm.hdu.edu.cn/showproblem.php?pid=1712
分析: 这是一个01背包的变体,但始终是01背包,只不过,每个物体的代价和利益的状态不唯一(也就是说花费不同的代价得到不同的利益). 所以只要在最里面再加一层循环,遍历每个物体的不代价和利益的状态即可!
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<iomanip>
using namespace std;
const int maxn=100000;
int map[105][105];
int dp[102];
int main(){
int n,m;
while(cin>>n>>m,n||m){
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
cin>>map[i][j];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;++i)
for(int k=m;k>=1;--k)
for(int j=1;j<=k;++j){
if(dp[k]<dp[k-j]+map[i][j])
dp[k]=dp[k-j]+map[i][j];
}
cout<<dp[m]<<endl;
}
return 0;
}