FZU 2214 ——Knapsack problem——————【01背包的超大背包】

2214 Knapsack problem

Accept: 6    Submit: 9
Time Limit: 3000 mSec    Memory Limit : 32768 KB

 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

 Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

题目大意:给你T组数据,每组有n个物品,一个背包容量B,每件有体积和价值。问你这个背包容纳的物品最大价值是多少。每个物品只能放入一次背包。

解题思路:首先这个很清楚看出来是个01背包。但是这个背包容量特别大,同时物品的体积很大,总的价值却很少。寻常意义上dp[i]表示背包容量为i时的最大价值,那么我们把这个dp[]数组的含义改变一下,dp[i]表示装价值为i时所需的最小容量。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 550;
const int INF = 0x3f3f3f3f;
int w[maxn], v[maxn];
int dp[5500];
int main(){
    int T, n, B;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&B);
        int V = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d%d",&w[i],&v[i]);
            V += v[i];
        }
        memset(dp,INF,sizeof(dp));
        dp[0] = 0;
        for(int i = 1; i <= n; i++){
            for(int j = V; j >= v[i]; j--){
                dp[j] = min(dp[j],dp[j-v[i]]+w[i]);
             //   printf("%d ",dp[j]);
            }
        }
        int ans = 0;
        for(int i = V; i >= 0; i--){
            if(dp[i] <= B){
                ans = i; break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-08-05 15:08:47

FZU 2214 ——Knapsack problem——————【01背包的超大背包】的相关文章

FZU 2214 Knapsack problem (01背包)

题意:给你n种物品,每种只有一个,第i种物品的价值为Vi,重量为Wi,把这些物品放入一个重量限制为B的背包中,使得背包内的物品在重量不超过B的前提下,价值尽量大,输出最大价值 1 <= n <= 500 1 <= B, w[i] <= 1000000000 1 <= v[1]+v[2]+...+v[n] <= 5000 思路:我们从范围中可以看到这个物品的重量和背包所能承受的重量特别大,我们不能使用常规的01背包,我们可以把问题转化一下,我们定义d(i,j)是把第i个物

FZU 2214 Knapsack 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

FZU 2214 Knapsack dp (转化背包)

就是一个背包裸题,由于物品的重量太大,开不了这么大的数组 所以转化一下,由于价值总和不大于5000,所以把价值看作重量,重量看作价值,那么就是同样的价值下,求一个最轻的重量 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib> #include<cmath> #include<cstdlib>

(01背包 当容量特别大的时候) Knapsack problem (fzu 2214)

http://acm.fzu.edu.cn/problem.php?pid=2214 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

B - 真&#183;签到题 FZU - 2214(背包)

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 tota

knapsack problem 背包问题 贪婪算法GA

knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按价值密度从大到小依次放入包内直到放不下,设此时放了s个物品 2.将所得价值与最大价值()所比较,取最大的作为输出 贪婪算法与最优解竞争比(近似比)为 证明:

有关货币问题的动态规划题目--有关01背包,完全背包,多重背包

背包dp:参考背包九讲以及给出一些题目 01背包 (先枚举物品,再逆序枚举容量) 给定n件物品和一个容量为V的背包,每件物品的体积是w[i],价值是va[i](1<=i<=n),求在不超过背包的容量的情况下,怎么选择装这些物品使得得到的价值最大? 例如:有5件物品,体积分别是{2,2,6,5,4},价值分别是{6,3,5,4,6} 递归式:F(i,v)=max(F(i-1,v), F(i-1,v-w[i])+va[i]),其中F(i,v)表示把前i种物品恰放入背包容量为v时取得的最大价值 把这

Mashmokh&#39;s Designed Problem 01生成树

题意 给一张无向连通图, 每条边可能是黑边或白边, 问是否存在一种生成树构造使得树上黑边数量 = 白边数量 ? $1\le n,m\le 100000$ . 分析 我们可以把 黑边 当做 权值为1的边 , 白边 当做 权值为0的边 . 这样如果存在一种生成树构造, 使得树上黑边数量 = 白边数量 , 意味着 $n$ 为奇数, 且生成树的边权之和为 $\frac{n-1}{2}$ . 问题转化为: 对于一张边权为 $0$ 或 $1$ 的无向连通图, 是否有一种生成树, 其边权之和恰好等于 $m$

HDU 3033 I love sneakers! (01背包+反分组背包)

题意:给你 n,m,k,表示有k种鞋子共n双,你有m的容量: 每双鞋子有容量p和价值v:问是否买全k种鞋子,若能在容量为m的情况下最多能买到鞋子的价值为多少: 每双鞋子只能买一次(01背包),每种鞋子至少买一种(分组背包:每组只能有一个)与传统分组背包的限制相反. 注意初始化!!! #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map&g