hdu 2602 简单的01背包

来源:互联网 发布:bugclose 源码下载 编辑:程序博客网 时间:2024/06/07 06:25

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2602

最简单的01背包,用一维数组优化。。

#include<stdio.h>#include<algorithm>#define maxn 1005using namespace std;int t,n,v;int wight[maxn];int value[maxn];int beibao[maxn];int main(){    scanf("%d",&t);   while(t--)   {       scanf("%d%d",&n,&v);       for(int x=0;x<n;x++)        scanf("%d",&value[x]);       for(int x=0;x<n;x++)        scanf("%d",&wight[x]);        for(int x=0;x<=v;x++)            beibao[x]=0;    for(int x=0;x<n;x++)        for(int y=v;y>=wight[x];y--)          beibao[y]=max(beibao[y],beibao[y-wight[x]]+value[x]);          printf("%d\n",beibao[v]);   }}