[HDU] [2602] Bone Collector

Problem Description

Loading...

Problem Analysis

Basic 0/1 Knapsack problem

There are also multiple ways to solve the Knapsack problem
This one records the maximum value achievable for each weight
After the search, dp[V] is the maximum value

Pay attention to initializing the array

I’ll probably write other solutions as well

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#include<string.h>
int v[1020],w[1020],dp[1020];
int max(int a,int b) {
	return a>b?a:b;
}
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int N,V;
		scanf("%d %d",&N,&V);
		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=V; j>=w[i]; j--) {
				dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
			}
		}
		printf("%d\n",dp[V]);
	}
	return 0;
}

Problem link:【杭电】[2602]Bone Collector

Loading...