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

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
#define N 2600000
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f

int v[550], w[550];
int dp[N];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int i, j, n, B, Max=0, Index;

        scanf("%d%d", &n, &B);

        for(i=1; i<=n; i++)
        {
            scanf("%d%d", &w[i], &v[i]);
            Max += v[i];
        }

        for(i=0; i<=Max; i++)
            dp[i] = INF;
        dp[0] = 0;
        for(i=1; i<=n; i++)
        for(j=Max; j>=v[i]; j--)
        {
           dp[j] = min(dp[j], dp[j-v[i]]+w[i]);
        }

        for(i=0; i<=Max; i++)
            if(dp[i]<=B) Index = i;

        printf("%d\n", Index);

    }
    return 0;
}
时间: 2024-10-11 07:56:54

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

HDU 2639 01背包求第k大

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3718    Accepted Submission(s): 1903 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par

【hdu3080】01背包(容量10^7)

[题意]n个物品,有wi和vi,组成若干个联通块,只能选取一个联通块,问得到m的价值时最小要多少空间(v).n<=50,v<=10^7 [题解] 先用并查集找出各个联通块. 这题主要就是v太大了,跟以往的背包不同. 我们回想01背包,f[j+v[i]]=max(f[j]+w[i]); 在这里面很明显很多状态都没有用. 优化:如果有2个状态,v1<=v2 && w1>=w2 则(v2,w2)这个状态是没有用的. 我们回到滚动数组中: f[i][j+v[i]]=max(

HDU - 2639(01背包求第K大值)

传送门 题意:it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum.  If the total number of different values is less than K,just ouput 0. 输入:T组数据 体积V 要求的K    N , V, K(N <= 100 , V <= 1000 , K <= 30) 各项价值 各项

01背包(容量为浮点数)

http://acm.hdu.edu.cn/showproblem.php?pid=1864 最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 36208    Accepted Submission(s): 11135 Problem Description 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A

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

2214 Knapsack problem Accept: 6    Submit: 9Time 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

csu 1547(01背包)

1547: Rectangle Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 996  Solved: 277[Submit][Status][Web Board] Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m)

poj 2184 0---1背包的变形

这题是0--1背包的变形,对理解0--1背包有很大的帮组 题意:要选一些牛去参见展览,每个牛有幽默.智慧两个选择标准,要求选的这些牛使得幽默和智慧的总和最大且幽默和智慧的每个总和都必须是大于等于0: 刚看的这个题目是时候,知道是一个0--1背包的的题目,但就是不知道怎么来写出状态转移方程,因为题中的两个变量都是有负值的. 看了大牛的解题报告才知道. 我们可以把幽默个变量看成是体积 , 智慧看成是价值. 我们可以把每个牛幽默的值 , 放在一个坐标上,让后整体往右移,使得最小值为 0 , 那么这时候

POJ Washing Clothes 洗衣服 (01背包,微变型)

题意:有多种颜色的衣服,由两个人合作来洗,必须洗完一种颜色才能洗下一种,求需要多少时间能洗完. 思路:将衣服按颜色分类,对每种颜色进行01背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最公平地平分工作量.因为一个先洗完就得等到另一人洗完.最后把洗完每种颜色的时长加起来返回.注:poj不允许用map,不然更省事,根据string和int做个哈希映射. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #includ

hihoCoder week6 01背包

01背包 题目链接 https://hihocoder.com/contest/hiho6/problem/1 #include <bits/stdc++.h> using namespace std; const int N = 500 + 10; int need[N], value[N]; int dp[100000+10]; int main () { int n,V; scanf("%d %d", &n, &V); for(int i=1;i<