【 FZU - 2214 】Knapsack problem(逆向0-1背包)

题干:

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[ j ]表示,买到价值j的物品所需要的最小质量。跑0-1背包就可以了。贴一道类似题目【nyoj - 860】 又见0-1背包

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
int w[505],v[505];
int dp[10000 + 5];
int n,m; 
int main()
{int t;scanf("%d",&t); while(t--) {scanf("%d%d",&n,&m); int sum = 0;for(int i = 1; i<=n; i++) {scanf("%d%d",&w[i],&v[i]);sum += v[i];}memset(dp,INF,sizeof(dp));dp[0] = 0;for(int i = 1; i<=n; i++) {for(int j = sum; j>=v[i]; j--) {dp[j] = min(dp[j],dp[j - v[i]] + w[i]) ;}}int ans = 0;for(int i = sum; i>=0; i--) {if(dp[i] <= m) {ans = i;break;} }printf("%d\n",ans);}return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441754.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

服务器2008系统如何设置休眠时间,Win7休眠和睡眠怎么开启(Win2008)

如果把 Win7休眠和睡眠关闭了&#xff0c;需要的时候可以用命令重新开启&#xff0c;毕竟这两个功能不但可以节约电&#xff0c;还可以迅速恢复工作状态&#xff0c;节约开机开软件的时间。Win2008 R2 跟 Win7 同一内核&#xff0c;开启休眠和睡眠的命令也一样。在开启休眠和睡…

【HDU - 1220】Cube (组合数学,简单)

题干&#xff1a; Cowl is good at solving math problems. One day a friend asked him such a question: You are given a cube whose edge length is N, it is cut by the planes that was paralleled to its side planes into N * N * N unit cubes. Two unit cubes may h…

java poi读取word中附件_Java POI导入word, 带图片

1.导入文件示例&#xff0c;word中简历表格模板2.代码示例分两部分&#xff0c;一部分读取图片/*** 导入word(基本信息&#xff0c;word格式)* param staffId* param baseInfoFile*/void importStaffInfo(Integer staffId, MultipartFile file);-- 读取图片InputStream inputSt…

如何将文件拷贝服务器上,如何将文件复制到云服务器上

如何将文件复制到云服务器上 内容精选换一换将文件上传至Windows云服务器一般会采用MSTSC远程桌面连接的方式。本节为您介绍本地Windows计算机通过远程桌面连接&#xff0c;上传文件至Windows云服务器的操作方法。Windows云服务器可以访问公网。在本地Windows计算机上&#xff…

mysql5.7解压版错误_mysql 5.7 解压版 安装net start mysql 发生系统错误 2

1.配置环境变量 用户变量path 添加 mysql 安装目录2.新建my.ini文件 放到E:\mysql-5.7.24-winx64安装目录下[mysqld]port 3306basedirC:/software/mysql-5.7.21-winx64datadirC:/software/mysql-5.7.21-winx64/datamax_connections200character-set-serverutf8default-storage…

【HDU - 2553】N皇后问题 (dfs经典问题,回溯,搜索)

题干&#xff1a; 在N*N的方格棋盘放置了N个皇后&#xff0c;使得它们不相互攻击&#xff08;即任意2个皇后不允许处在同一排&#xff0c;同一列&#xff0c;也不允许处在与棋盘边框成45角的斜线上。 你的任务是&#xff0c;对于给定的N&#xff0c;求出有多少种合法的放置方…

浪潮服务器建立虚拟驱动器,像《十二时辰》一样去建立标准! 浪潮这款服务器做到了...

原标题&#xff1a;像《十二时辰》一样去建立标准&#xff01; 浪潮这款服务器做到了这个夏天&#xff0c;《长安十二时辰》制霸屏幕开画至今豆瓣评分达到8.8分现已成功“出海”在Amazon、Youtube、Viki付费上线成为唐风古韵的又一风向标现如今&#xff0c;越洋的标准可不止悠悠…

【HDU - 5918 】Sequence I (数组(字符串)匹配问题,可选KMP)

题干&#xff1a; Mr. Frog has two sequences a1,a2,⋯,ana1,a2,⋯,an and b1,b2,⋯,bmb1,b2,⋯,bm and a number p. He wants to know the number of positions q such that sequence b1,b2,⋯,bmb1,b2,⋯,bm is exactly the sequence aq,aqp,aq2p,⋯,aq(m−1)paq,aqp,aq2p,…

mysql_fetch_bit_mysql_fetch_array()

mysql_fetch_array()(PHP 4, PHP 5)从结果集中取得一行作为关联数组&#xff0c;或数字数组&#xff0c;或二者兼有说明mysql_fetch_array(resource$result[,int$ result_type]):array返回根据从结果集取得的行生成的数组&#xff0c;如果没有更多行则返回FALSE。mysql_fetch_a…

【HDU - 5916】Harmonic Value Description (构造,思维,SJ题)

题干&#xff1a; The harmonic value of the permutation p1,p2,⋯pnp1,p2,⋯pn is ∑i1n−1gcd(pi.pi1)∑i1n−1gcd(pi.pi1) Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n]. Inpu…

在python中、对于函数定义代码的理解_python中如何理解装饰器代码?

长文预警&#xff0c;【最浅显易懂的装饰器讲解】能不能专业地复制题目&#xff1f;配上代码&#xff0c;问题分段。我来给提主配上问题的代码。正式回答&#xff1a;1&#xff1a;如何理解return一个函数&#xff0c;它与return一个值得用法区别在哪&#xff1f;敲黑板&#x…

【AtCoder - 2554】Choose Integers (找规律,或枚举)

题干&#xff1a; Problem Statement We ask you to select some number of positive integers, and calculate the sum of them. It is allowed to select as many integers as you like, and as large integers as you wish. You have to follow these, however: each sele…

1m带宽可以做mysql数据库吗_服务器的1M带宽够用吗?1M网速是多少?

1M是什么&#xff1f;通常&#xff0c;在计算机中1M表示的是计算机的内存容量&#xff0c;即1M1024KB。但有不同的含义&#xff0c;云服务器中1M表示服务器的带宽&#xff0c;即1M带宽。在服务器中1M带宽够用吗&#xff1f;云服务器的1兆网速是多少&#xff1f;首先把云服务器带…

【POJ - 2318】TOYS(计算几何,叉积判断点与直线位置关系,二分)

题干&#xff1a; Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys i…

esp32 micropython spiffs_spiffs 文件系统在esp32中的应用

spiffs 介绍SPIFFS 是一个开源文件系统&#xff0c;用于 SPI NOR flash 设备的嵌入式文件系统&#xff0c;支持磨损均衡、文件系统一致性检查等功能。spiffs 源码地址​github.comspiffs 特点而我们知道乐鑫的esp32的大部分存储都依赖于SPI flash &#xff0c;spiffs可以说对于…

【HDU - 1968】【UVA - 12096】The SetStack Computer (模拟,集合求交集并集操作,STL实现)

题干&#xff1a; Background from Wikipedia: 揝et theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational…

info testing mysql_SQLMASQLMAP中文说明(linux版本)

这个东西&#xff0c;是mickey整理的&#xff0c;不多说了&#xff0c;尊重一下原作者&#xff0c;转载注明mickey整理就好了更新svn checkout https://svn.sqlmap.org/sqlmap/trunk/sqlmap sqlmap-devsqlmap.py -u "http://www.islamichina.com/hotelinchina.asp?cityid…

关于C++里面使用set_union,set_intersections、set_merge、set_difference、set_symmetric_difference等函数的使用总结

set里面有set_intersection&#xff08;取集合交集&#xff09;、set_union&#xff08;取集合并集&#xff09;、set_difference&#xff08;取集合差集&#xff09;、set_symmetric_difference&#xff08;取集合对称差集&#xff09;等函数。其中&#xff0c;关于函数的五个…

java mouseenter_关于事件mouseover ,mouseout ,mouseenter,mouseleave的区别

最近在做的在线考试和课程商城都遇到这样的问题&#xff1a;就是鼠标滑过的时候出现一个层&#xff0c;当鼠标滑到当前层的话mouseover和mouseout在低版本的浏览器会出现闪动的现象&#xff0c;解决这个现象的办法有许多&#xff0c;不过我觉得有一种是最简单的那就是把mouseov…

【HDU - 1465 】不容易系列之一 (组合数学,错排)

题干&#xff1a; 大家常常感慨&#xff0c;要做好一件事情真的不容易&#xff0c;确实&#xff0c;失败比成功容易多了&#xff01; 做好“一件”事情尚且不易&#xff0c;若想永远成功而总从不失败&#xff0c;那更是难上加难了&#xff0c;就像花钱总是比挣钱容易的道理一…