01背包裸题,我就不赘述了,……01背包不会的自己去学背包九讲吧,太水了啊啊啊啊。
第20行写渣了,if判断直接加到for循环边界里就好了。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 3403
#define M 12881
using namespace std;
int n,m;
int f[M];
int main()
{
// freopen("test.in","r",stdin);
int i,j,p,a,b;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
for(j=m;j>=0;j--)if(j+a<=m)
{
f[j+a]=max(f[j+a],f[j]+b);
}
}
for(p=i=0;i<=m;i++)p=max(p,f[i]);
printf("%d\n",p);
return 0;
}

本文介绍了一个经典的01背包问题,并提供了一份详细的C++实现代码。通过对代码的逐行解析,帮助读者理解如何通过迭代更新状态转移方程来求解最大价值。同时,文中还提到了一些代码优化技巧。
2314

被折叠的 条评论
为什么被折叠?



