hdu2602: http://acm.hdu.edu.cn/showproblem.php?pid=2602
题意:有n个物品和容量为v的背包,每种物品只可取一个,且有不同价值及体积,求最大价值
解法:01背包
code:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int v[1010],w[1010],f[1010];
int main()
{
int t,n,c,i,j,k;
scanf("%d",&t);
for(k=0;k<t;k++)
{
scanf("%d%d",&n,&c);
for(i=0;i<n;i++)
scanf("%d",&w[i]);
for(i=0;i<n;i++)
scanf("%d",&v[i]);
memset(f,0,sizeof(f));
for(i=0;i<n;i++)
{
for(j=c;j>=0;j--) //逆序
if(j>=v[i])
f[j]=max(f[j],f[j-v[i]]+w[i]);
}
printf("%d\n",f[c]);
}
}
/*
input:
1
5 10
1 2 3 4 5
5 4 3 2 1
output:
14
*/