Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the total value is as large as possible. Find the maximum total value. (Note that each item can be only chosen once).
Input
The first line contains the integer T indicating to the number of test cases.
For each test case, the first line contains the integers n and B.
Following n lines provide the information of each item.
The i-th line contains the weight w[i] and the value v[i] of the i-th item respectively.
1 <= number of test cases <= 100
1 <= n <= 500
1 <= B, w[i] <= 1000000000
1 <= v[1]+v[2]+...+v[n] <= 5000
All the inputs are integers.
Output
For each test case, output the maximum value.
Sample Input
1
5 15
12 4
2 2
1 1
4 10
1 2
Sample Output
15
题意:
给定一组n个项目,每个项目具有权重w[i]和值v[i],确定一种将项目选择到背包中的方法,使得总重量小于或等于给定极限B,并且总值尽可能大。求最大值。(注意每个项目只能选择一次)。
这个就是01背包的题目,按照常理就是通过重量来找价值,看一个物品是否放到背包中让价值最大。
for(int i = 1; i <= n; i++)
for(int j = B; j >= w[i]; j--)
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
但是由于这个题目中给出的B范围太大,1 <= B <= 1000000000,所以不能按照这个方法。
那么就可以转换过来,统计所有的物品的价值,通过价值来找重量,当重量小于B并最接近B时取此时的价值。
for(int i = 1; i <= n; i++)
for(int j = sumv; j >= v[i]; j--)
dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
然后找出dp数组中小于B并且最接近B的元素的下标,即为最大价值。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
int v[505], w[505], n, maxv;
ll dp[maxn], B;
int main(){
int T;
scanf("%d", &T);
while(T--){
maxv = 0;
scanf("%d %d", &n, &B);
for(int i = 1; i <= n; i++) {
scanf("%d %d", &w[i], &v[i]);
maxv += v[i]; //统计出总价值
}
for(int i = 1; i <= maxv; i++) dp[i] = 1e17;
dp[0] = 0;
for(int i = 1; i <= n; i++){
for(int j = maxv; j >= v[i]; j--){
dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
}
}
ll ans = inf;
for(int i = maxv; i >= 0; i--){
if(dp[i] <= B) {
ans = i; break;
}
}
printf("%lld\n", ans);
}
return 0;
}