http://acm.fzu.edu.cn/problem.php?pid=2214

Accept: 4    Submit: 6
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

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

分析:虐的没点脾气,首先B很大,用01背包开数组开不开,然后我就想什么离散化,结果也没搞出来,最后看到题解居然是把价值下的重量,顿时感觉自己弱爆了,可以把价值看作背包容量啊,就是把这个价值装满的最小重量就是那个对应的重量下的最大价值,一点点变性思维就能解决的事,弱爆了
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
long long thing[];
int w[],v[];
int main()
{
int t,n,B;
scanf("%d", &t);
while(t--)
{
int sum = ;
scanf("%d%d", &n,&B);
for(int i = ; i <= n; i++)
{
scanf("%d%d", &w[i], &v[i]);
sum += v[i];
}
memset(thing, INF, sizeof(thing));
thing[] = ;
for(int i = ; i <= n; i++)
{
for(int j = sum; j >= v[i]; j--)
{
if(thing[j - v[i]] != INF)
thing[j] = min(thing[j], thing[j - v[i]] + w[i]);
}
}
for(int i = sum; i >= ; i--)
{
if(thing[i] <= B)
{
printf("%d\n",i);
break;
}
}
}
return ;
}

FOJProblem 2214 Knapsack problem(01背包+变性思维)的更多相关文章

  1. FZU - 2214 Knapsack problem 01背包逆思维

    Knapsack problem Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...

  2. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  3. FZU Problem 2214 Knapsack problem(背包+思维转换)

    转化思维,把价值当成背包容量,选择最小的花费,从上到下枚举,找到当这个最小的花费. #include<iostream> #include<cstring> #include& ...

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

    2214 Knapsack problem Accept: 6    Submit: 9Time Limit: 3000 mSec    Memory Limit : 32768 KB  Proble ...

  5. 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 t ...

  6. Problem 2214 Knapsack problem 福建第六届省赛

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题目大意:给你T组数据,每组有n个物品,一个背包容量B,每件有体积和价值.问你这个背包容纳的物品最大价值 ...

  7. lightoj 1125【01背包变性】

    题意: 从n个数里选出m个来,还要使得这m个数之和被d整除. 给一个n和q,再给n个数,再给q个询问,每个询问包含两个数,d,m; 对于每个case输出每个q个询问的可行的方案数. 思路: 每个数只能 ...

  8. Educational DP Contest E - Knapsack 2 (01背包进阶版)

    题意:有\(n\)个物品,第\(i\)个物品价值\(v_{i}\),体积为\(w_{i}\),你有容量为\(W\)的背包,求能放物品的最大价值. 题解:经典01背包,但是物品的最大体积给到了\(10^ ...

  9. HDU 2955 01背包(思维)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. ArcGis 获取地理、平面坐标系

                                         ESRI.ArcGIS.Geometry.ISpatialReference spatialReference = spati ...

  2. Netty指定分隔的字符

    package org.zln.netty.five.part02; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; ...

  3. android 合并两个jar包

    你所要导出的类里边用到了别的jar包.比如说你写的类连接了数据库,用到数据库驱动包oracl.jar(也就是你导入到Myeclipse或eclipse的jdbc包).. .在dos环境下,进入到D盘的 ...

  4. 【转】【Http】Http各种错误的意思

    IIS状态代码的含义 当用户试图通过HTTP或文件传输协议(FTP)访问一台正在运行Internet信息服务(IIS)的服务器上的内容时,IIS返回一个表示该请求的状态的数字代码.该状态代码记录在II ...

  5. 记2012-2013年一路的Windows Phone历程

    昨天和秦春林同学小聚一次,不厌其烦的唠叨各种认识了许多年的纪念,感觉他仍然保持着那份对技术的追求和梦想,而我却已经变得逐利,每个人享受幸福的方式不一样,因此追逐幸福的过程也就是没有办法一样了,你无法知 ...

  6. Ajax实现异步操作实例_针对XML格式的请求数据

    js分类中有一节[原生js异步请求,XML解析]主要说明了js前台是如何处理XML格式请求和如何接受由服务器返回的XML数据的解析,今天我将用一个实例来说明具体要如何操作. 前台的参数类型也是XML使 ...

  7. [CareerCup] 6.5 Drop Eggs 扔鸡蛋问题

    6.5 There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. I ...

  8. [MetaHook] Load TGA texture to OpenGL

    This function load a *.tga texture file and convert to OpenGL pixel format, uncompress only. #pragma ...

  9. iOS开发之—— XCODE真机调试设备连接一直忙碌如何处理!(真机调试各种错误提示解决)

    真机调试,想连接真机调试代码可是连上设备后就一直转圈, 在Divice里面一直提示“iphone名称” is busy: Processing symbol files Xcode will cont ...

  10. Object C学习笔记16-委托(delegate)

    在.NET中都知道委托(delegate),通俗点的解释就是可以将方法作为一个参数传到另外一个方法中使用. 委托是一种引用方法的类型.一旦为委托分配了方法,委托将与该方法具有完全相同的行为.委托方法的 ...