设f[i][j]表示考虑完前i个物体,背包的容量为j(物体的总体积小于等于j)时的最大价值
答案就是f[n][m]
边界f[0][...] = 0
转移:决策一:选f[i][j]->f[i+1][j+weight[i+1]]
不选f[i][j]->f[i+1][j];
代码:
#include<iostream>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstdio>
using namespace std;
const int MAXN = 10000;
int n, m;//n为物品数量 m为背包容量 m <= 200 n <= 30
int Wi[MAXN], Ci[MAXN]; //Wi重量,Ci价值;//int Wi, Ci;
int laji[MAXN][MAXN];//int laji[MAXN];
int main( )
{
scanf("%d %d", &m, &n);
for(int i = 1;i <= n; i++)
{
scanf("%d %d", &Wi[i], &Ci[i]);
}
for(int i = 1;i <= n;i++)
{
for(int j = 0;j <= m;j++)
{
laji[i][j] = laji[i - 1][j];
if(j >= Wi[i])
{
laji[i][j] = max(laji[i - 1][j], laji [i - 1][j - Wi[i]] + Ci[i]);
}
}
}
printf("%d", laji[n][m]);
}