FZU-2214 Knapsack problem(DP使用)
Accept: 863 Submit: 3347
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
5 15
12 4
2 2
1 1
4 10
1 2
Sample Output
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
#include<iostream>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int w[505];
int v[505];
long long dp[5005];
int n,m; int main(){
int t;
std::ios::sync_with_stdio(false);
cin>>t;
while(t--){
cin>>n>>m;
int sum=0;
for(int i=0;i<n;i++){
cin>>w[i]>>v[i];
sum+=w[i];
}
memset(dp,INF,sizeof(dp));
dp[0]=0;
for(int i=0;i<n;i++){
for(int j=5000;j>=0;j--){///价值
if(j>=v[i]){
dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
dp[j]=dp[j]==0?(dp[j-v[i]]+w[i]):min(dp[j],dp[j-v[i]]+w[i]);
///存重量
} }
}
int ans=0;
for(int i=0;i<=5000;i++){
// dp[i]=0x3f3f3f3f-dp[i];
// cout<<i<<" "<<dp[i]<<endl;
if(dp[i]<=m&&i>ans){
ans=i;
}
}
cout<<ans<<endl;
}
}
FZU-2214 Knapsack problem(DP使用)的更多相关文章
- FZU 2214 ——Knapsack problem——————【01背包的超大背包】
2214 Knapsack problem Accept: 6 Submit: 9Time Limit: 3000 mSec Memory Limit : 32768 KB Proble ...
- 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 ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- 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 ...
- FOJProblem 2214 Knapsack problem(01背包+变性思维)
http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4 Submit: 6Time Limit: 3000 mSec Memory Lim ...
- Problem 2214 Knapsack problem 福建第六届省赛
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题目大意:给你T组数据,每组有n个物品,一个背包容量B,每件有体积和价值.问你这个背包容纳的物品最大价值 ...
- FZU 2214 Knapsack dp (转化背包)
就是一个背包裸题,由于物品的重量太大,开不了这么大的数组 所以转化一下,由于价值总和不大于5000,所以把价值看作重量,重量看作价值,那么就是同样的价值下,求一个最轻的重量 #include<c ...
- FZU Problem 2214 Knapsack problem(背包+思维转换)
转化思维,把价值当成背包容量,选择最小的花费,从上到下枚举,找到当这个最小的花费. #include<iostream> #include<cstring> #include& ...
- HNU 13108 Just Another Knapsack Problem DP + Trie树优化
题意: 给你一个文本串,和一些模式串,每个模式串都有一个价值,让你选一些模式串来组成文本串,使获得的价值最大.每个模式串不止能用一次. 思路: 多重背包,枚举文本串的每个位置和模式串,把该模式串拼接在 ...
随机推荐
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目7
2014-03-18 01:55 题目:给定一个MxN矩阵,如果某个元素为0,则将对应的整行和整列置为0. 解法:单独挑出一行和一列作为标记数组.因为某元素为0就全部置为0,所以不论A[i][j]为0 ...
- .net面试那些事
2014-3-30这天从昆明来到了江苏无锡,一周时间,不同地方不同感触. 在经过长达35小时的火车,在上海南见到了我弟,在经过两小时汽车到了无锡.回想起来....在坐汽车途中,虽然自己已很累,但从上海 ...
- Mac 电脑鼠标和触摸板滚动方向不一致的问题【已解决】
当我们使用鼠标连接到 MacBook 时,会发现无论怎么设置,鼠标和触摸板的滚动方向都是相反的,导致不能同时使用鼠标和触摸板 解决方法: 我安装了下面的程序,它只允许您反转鼠标的滚动行为: Scrol ...
- 洛谷P1101单词方阵
题目描述 给一n×n的字母方阵,内可能蕴含多个“yizhong”单词.单词在方阵中是沿着同一方向连续摆放的. 摆放可沿着 8个方向的任一方向,同一单词摆放时不再改变方向,单词与单词之间可以交叉,因此有 ...
- 重现ssd遇到的问题
首先是create_list.sh和create_data.sh中的data_dir的路径得修改. 然后是在create_data.sh文件调用$caffe_root下的scripts目录中的crea ...
- Linux中awk后面的RS, ORS, FS, OFS 含义
转载自http://blog.csdn.net/qq416647781/article/details/40649419 一.RS 与 ORS 差在哪 我们经常会说,awk是基于行列操作文本的 ...
- canvas 基础
1.<canvas>元素 <canvas id="tutorial" width="150" height="150"&g ...
- Spring 事件机制
通过模拟邮件的发送,说明Spring的事件监听机制 事件类 package org.zln.module_chapter2.event; import org.springframework.cont ...
- HDU 4746 HDOJ Mophues 2013杭州网赛I题
比赛的时候就预感到这题能出,但是会耗时比较多.结果最后是出了,但是有更简单的题没出. 是不是错误的决策呢?谁知道呢 题目意思: 定义f(x) = x分解质因数出来的因子个数 如 x = p0 * p0 ...
- POJ3345 Bribing FIPA 【背包类树形dp】
题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...