FZU-2214-Knapsack problem【01背包】

**

Knapsack problem

**
Problem Description

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

题目链接:FZU-2214

题目大意:n个物品,w容量取价值最大。01背包,但是w太大,所以需要转换一下

题目思路:dp[i][j]表示到第i个物品为止,价值为j的容量最小值

以下是代码:

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include<map>
using namespace std;
long long dp[505][5005]; //w最小
int v[5005];
long long w[5005];
#define INF 1000000707
int main(){
    int t;
    cin >> t;                       
    while( t-- ){
        int n,b;
        cin >> n >> b;
        memset(dp,0,sizeof(dp));
        int sum = 0;
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            cin >> w[i] >> v[i];
            sum += v[i];
        }
        for (int i = 0; i <= n; i++)
        {
            for( int j=0 ; j<=sum ; j++ )
            {
                    dp[i][j] = INF;
            }
        }
        for (int i = 0; i <= n; i++) dp[i][0] = 0;

        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j < v[i]; j++)
            {
                dp[i][j] = dp[i - 1][j];
            }
            for (int j = v[i]; j <= sum ; j++ )
            {
                dp[i][j] = dp[i - 1][j];
                if (dp[i - 1][j - v[i]] == INF) continue;
                dp[i][j] = min(dp[i][j],dp[i - 1][j - v[i]] + w[i]);
                if (dp[i][j] <= b)
                {
                    ans = max(ans,j);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

滚动数组:

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include<map>
using namespace std;
long long dp[5005]; //w最小
int v[505];
long long w[505];
#define INF 1000000707
int main(){
    int t;
    cin >> t;
    while( t-- ){
        int n,b;
        cin >> n >> b;
        memset(dp,0,sizeof(dp));
        int sum = 0;
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            cin >> w[i] >> v[i];
            sum += v[i];
        }
        for( int j=0 ; j<=sum ; j++ )
        {
            dp[j] = INF;
        }
        dp[0] = 0;
     // for (int i = 0; i <= n; i++) dp[i][0] = 0;

        for (int i = 1; i <= n; i++)
        {
            for (int j = sum; j >= v[i] ; j--)
            {
                if (dp[j - v[i]] == INF) continue;
                dp[j] = min(dp[j],dp[j - v[i]] + w[i]);
                if (dp[j] <= b)
                {
                    ans = max(ans,j);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

你可能感兴趣的:(dp,FZU,2214)