HDU 2602 - Bone Collector(01背包)

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

【思路】
01背包问题

#include<bits/stdc++.h>
using namespace std;

const int maxn=1005;

int n,W;
int w[maxn],v[maxn];
int dp[maxn];

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&W);
		for(int i=0;i<n;++i) scanf("%d",&v[i]);
		for(int i=0;i<n;++i) scanf("%d",&w[i]);
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n;++i){
			for(int j=W;j>=w[i];--j)
				dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
		}
		printf("%d\n",dp[W]);
	}
	return 0;
}
posted @ 2018-10-11 10:50  不想吃WA的咸鱼  阅读(66)  评论(0编辑  收藏  举报