Knapsack problem FZU - 2214 (01背包变形)

13人阅读 评论(0) 收藏 举报
分类:

Knapsack problem

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 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

我们用dp针对不同的价值计算最小的重量

当时想到了这样转变就是不知道怎么初始化了结果就没做出来哎

初始化为INF表示不存在而且由于前0个物品中什么都挑选不了,所以初始化dp[0] = 0;

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
int v[1010],w[1010];
int dp[5050];//dp[i]表示价值为i时的最小重量
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,W;
        scanf("%d%d",&n,&W);
        int V = 0;
        for(int i = 0; i < n; i++){
            scanf("%d%d",&w[i],&v[i]);
            V += v[i];
        }
        //初始化(很重要!)
        memset(dp,INF,sizeof(dp));
        dp[0] = 0;
        for(int i = 0; i < n; i++){
            for(int j = V; j >= v[i]; j--){
                dp[j] = min(dp[j],dp[j-v[i]]+w[i]);
            }
        }
        for(int i = V; i >= 0; i--){
            if(dp[i] <= W){
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}


查看评论

高级特效-多边形特效/PS高端辅助/动态人像速成特效 [精品推荐]

-
  • 1970年01月01日 08:00

Knapsack problem(FZU2214)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题面是最正常的背包,但是背包重量大到让人思考人生。 说真的第一看见这样的题目,比赛的时候看见空间...
  • tinyguyyy
  • tinyguyyy
  • 2016-04-18 17:23:05
  • 237

FZU-2214-Knapsack problem【01背包】

Knapsack problem
  • loy_184548
  • loy_184548
  • 2016-04-17 22:08:38
  • 450

FZU Problem 2214 Knapsack problem(01背包,超大背包)——第六届福建省大学生程序设计竞赛-重现赛

FZU Problem 2214 Knapsack problem(01背包,超大背包)——第六届福建省大学生程序设计竞赛-重现赛...
  • queuelovestack
  • queuelovestack
  • 2016-09-08 15:33:36
  • 523

FZU 2214 Knapsack problem

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题        意:给你一个背包,让你往里面尽量加价值总和多的物品。 思        路:是个0...
  • Seraphimon
  • Seraphimon
  • 2015-12-28 13:00:25
  • 443

FOJ-2214 Knapsack problem

FOJ-2214 Knapsack problem Problem Description Given a set of n items, each with a weight w[i] an...
  • hpulw
  • hpulw
  • 2016-03-01 11:33:25
  • 259

FOJ2214 Knapsack problem(逆01背包)

Link:http://acm.fzu.edu.cn/problem.php?pid=2214 Problem 2214 Knapsack problem Accept: 74...
  • Enjoying_Science
  • Enjoying_Science
  • 2017-11-26 00:17:04
  • 101

FZU - 2214 Knapsack problem (01背包变形)

题意:明显的01背包问题,但是背包容量很大很大,开不了那么大的数组,我们可以转变一下思维,正常的01背包dp[i]表示背包容量不超过i的情况下最大价值,但是这里表示能装下i价值的物品所需要的最小容量。...
  • GYH0730
  • GYH0730
  • 2018-04-15 19:10:42
  • 6

2015福建省赛 fzoj Knapsack problem 2214 (01背包&转换)

Problem 2214 Knapsack problem Accept: 6    Submit: 9 Time Limit: 3000 mSec    Memory Limit : 32768 ...
  • yanghui07216
  • yanghui07216
  • 2015-12-27 20:44:27
  • 315

FZU 2214 Knapsack problem 01背包变形

FZU 2214 Knapsack problem 01背包变形 数组越界处理 变量互换
  • qq_26071477
  • qq_26071477
  • 2016-08-20 23:23:33
  • 309
    个人资料
    持之以恒
    等级:
    访问量: 3万+
    积分: 4134
    排名: 9095
    文章存档
    最新评论