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

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



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
struct node
{
int u,v;
}num[10010];
int dp[10010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m,n;
scanf("%d%d",&n,&m);
memset(dp,INF,sizeof(dp));
int V=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&num[i].u,&num[i].v);
V+=num[i].v;
}
dp[0]=0;
for(int i=0;i<n;i++)
{
for(int j=V;j>=num[i].v;j--)
{
dp[j]=min(dp[j],dp[j-num[i].v]+num[i].u);
}
}
for(int j=V;j>=0;j--)
{
if(dp[j]<=m)
{
printf("%d\n",j);
break;
}
}
}
return 0;
}

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. cogs 2060. 除法表达式

    2060. 除法表达式 ★★   输入文件:baoquansl.in   输出文件:baoquansl.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 经过无尽的蘑菇和 ...

  2. n个骰子,和为x的概率分别是多少

    开始我居然又没有想出来.. 还是看了解法.开始的时候,一直想的是用概率,百分比来求,后来才发现,用次数来求,最后除一下,更近清晰. 方法,可以是递归,每次多一个骰子的时候,次数分别加上个数以及上一次i ...

  3. POJ 3695

    可以用容斥原理来求.求两个矩形的并的时候可以使用条件 x1=max(p.x1,q.x1);y1=max(p.y1,q.y1);x2=min(p.x2,q.x2);y2=min(p.y2,q.y2); ...

  4. HDU 5416 CRB and Tree (2015多校第10场)

    欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536 ...

  5. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  6. node--19 moogose demo1

    db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...

  7. matplotlib 可视化 —— matplotlib.patches

    官方帮助文档 patches - Matplotlib 1.5.1 documentation patches 下主要包含的常用图形类有: Eclipse Circle Wedge 1. plt.gc ...

  8. 15-11-24 system同步与异步

    //打开关闭qq #define _CRT_SECURE_N0_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #i ...

  9. POJ 2132 暴搜OR Floyd

    题意: 给你一个邻接矩阵(n<=25)问所有1到2路径的gcd的lcm是多少. 一些经验(WA/TLE的经验): 1. 无脑暴搜 是会TLE的--. 2. 关于精度 dyf神牛说了:long l ...

  10. xshell同时发送多条命令

    1.如图,勾选撰写栏 勾选全部会话 下面的是一个很方便的小技巧.分屏 如下图垂直排列 水平排列等等