Problem 2214 Knapsack problem

Accept: 5    Submit: 8
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 42 21 14 101 2

Sample Output

15

Source

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



  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. #define INF 0x3f3f3f
  6. struct node
  7. {
  8. int u,v;
  9. }num[10010];
  10. int dp[10010];
  11. int main()
  12. {
  13. int t;
  14. scanf("%d",&t);
  15. while(t--)
  16. {
  17. int m,n;
  18. scanf("%d%d",&n,&m);
  19. memset(dp,INF,sizeof(dp));
  20. int V=0;
  21. for(int i=0;i<n;i++)
  22. {
  23. scanf("%d%d",&num[i].u,&num[i].v);
  24. V+=num[i].v;
  25. }
  26. dp[0]=0;
  27. for(int i=0;i<n;i++)
  28. {
  29. for(int j=V;j>=num[i].v;j--)
  30. {
  31. dp[j]=min(dp[j],dp[j-num[i].v]+num[i].u);
  32. }
  33. }
  34. for(int j=V;j>=0;j--)
  35. {
  36. if(dp[j]<=m)
  37. {
  38. printf("%d\n",j);
  39. break;
  40. }
  41. }
  42. }
  43. return 0;
  44. }

FZOJ--2214--Knapsack problem(背包)的更多相关文章

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

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

  2. FZU 2214 Knapsack problem 01背包变形

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

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

  4. FOJProblem 2214 Knapsack problem(01背包+变性思维)

    http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4    Submit: 6Time Limit: 3000 mSec    Memory Lim ...

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

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

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

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

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

  8. FZU-2214 Knapsack problem(DP使用)

    Problem 2214 Knapsack problem Accept: 863    Submit: 3347Time Limit: 3000 mSec    Memory Limit : 327 ...

  9. knapsack problem 背包问题 贪婪算法GA

    knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...

随机推荐

  1. ASP.NET-AuthorizeAttribute做身份验证操作

    代码顺序为:OnAuthorization-->AuthorizeCore-->HandleUnauthorizedRequest 如果AuthorizeCore返回false时,才会走H ...

  2. Linux进程的内存布局

    这张图很好,注意其中最上面是高位地址,虽然很多个0,但是c开头的,不要看反了: 更具体的可以看这里: A.正文段.这是由cpu执行的机器指令部分.通常,正文段是可共享的,所以即使是经常执行的程序(如文 ...

  3. [ReactVR] Animate Text, Images, Views, and 3D Elements Using the Animated Library in React VR

    Motion is an important aspect of a complete immersive experience, therefor we are going to look into ...

  4. jquery outerHeight方法 outerWidth方法 获取元素实际宽度高度

    曾经写代码中,每当须要获取元素的实际"宽度"(这里的宽度是指元素宽度加上其边距)时,都须要用元素宽度加上margin值才行,今天发现一个叫outerWidth(options)的方 ...

  5. Process Monitor

    https://en.wikipedia.org/wiki/Process_Monitor Process Monitor is a free tool from Windows Sysinterna ...

  6. iOS-MBProgressHUD使用

    在码代码过程中,我们经常用到MBProgressHUD,但我很少实例化使用,一般都是偷个懒直接显示隐藏,这里贴上详解,以便日后有样式要求时使用. 1,MBProgressHUD常用属性和用法Demo ...

  7. hdoj--1950--Bridging signals(二分查找+LIS)

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. vue-cli全引入jquery

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 vue-cli全引入jquery:(vue-cli使用webpack) 第一步: 在package.json文件里的de ...

  9. Linux下处理JSON的命令行工具:jq---安装

    转自:https://blog.csdn.net/Sunny_much/article/details/50668871      JSON是前端编程经常用到的格式.Linux下也有处理处理JSON的 ...

  10. 4.git "Could not read from remote repository.Please make sure you have the correct access rights."解决方案

    转自:https://zhiku8.com/git-could-not-read-from-remote-repository.html 我们在使用git clone 或其他命令的时候,有时候会遇到这 ...