NYIST 860 又见01背包
又见01背包
时间限制:1000 ms | 内存限制:65535 KB
难度:3
描述
有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W
的物品,求所有挑选方案中物品价值总和的最大值。
1 <= n <=100
1 <= wi <= 10^7
1 <= vi <= 100
1 <= W <= 10^9
输入
多组测试数据。
每组测试数据第一行输入,n 和 W ,接下来有n行,每行输入两个数,代表第i个物品的wi 和 vi。
输出
满足题意的最大价值,每组测试数据占一行。
样例输入
4 5
2 3
1 2
3 4
2 2
样例输出
7
来源
飘谊系列
上传者
TC_张友谊
解题:用价值换重量,尽可能多的价值换取尽可能少的重量。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int dp[maxn*maxn],v[maxn],w[maxn],n,W,V;
int main() {
while(~scanf("%d %d",&n,&W)) {
V = ;
for(int i = ; i <= n; ++i) {
scanf("%d %d",w+i,v+i);
V += v[i];
}
memset(dp,0x3f,sizeof(dp));
dp[] = ;
for(int i = ; i <= n; ++i) {
for(int j = V; j >= v[i]; --j)
dp[j] = min(dp[j],dp[j-v[i]]+w[i]);
}
for(int i = V; i >= ; --i)
if(dp[i] <= W) {
printf("%d\n",i);
break;
}
}
return ;
}
NYIST 860 又见01背包的更多相关文章
- [NYOJ 860] 又见01背包
又见01背包 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W 的物品,求所 ...
- NYOJ:题目860 又见01背包
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 方法一:不用滚动数组(方法二为用滚动数组,为方法一的简化) 动态规划分析:最少要拿总 ...
- nyoj860 又见01背包(背包变形)
题目860 pid=860" style="text-decoration:none; color:rgb(55,119,188)">题目信息 执行结果 本题排行 ...
- 【志银】NYOJ《题目860》又见01背包
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 方法一:不用滚动数组(方法二为用滚动数组,为方法一的简化) 动态规划分析:最少要拿总 ...
- NYOJ--860 又见01背包(01背包)
题目http://acm.nyist.net/JudgeOnline/problem.php?pid=860 分析:题目和普通的01背包问题一样,但是唯一不同的是数据的特殊性. 如果10^9根本就开辟 ...
- hdu3448 01背包+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound f ...
- nyoj 203 三国志 dijkstra+01背包
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=203 思路:先求点0到每个点的最短距离,dijkstra算法,然后就是01背包了 我奇怪的 ...
- Codeforces 741B:Arpa's weak amphitheater and Mehrdad's valuable Hoses(01背包+并查集)
http://codeforces.com/contest/741/problem/B 题意:有 n 个人,每个人有一个花费 w[i] 和价值 b[i],给出 m 条边,代表第 i 和 j 个人是一个 ...
- hdu 2639 Bone Collector II (01背包,求第k优解)
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...
随机推荐
- HDU5514 Frogs
/* HDU5514 Frogs http://acm.hdu.edu.cn/showproblem.php?pid=5514 容斥原理 * * */ #include <cstdio> ...
- C#基础概念 代码样例
C# int与string一起操作时注意 1 int a1= 1; 2 string a2= "2"; 3 Console.WriteLine(a1+a2); 4 Console. ...
- 大菲波数 【杭电-HDOJ-1715】 附题+具体解释
/* 大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- PHP别名引用错误:“The use statement with non-compound name … has no effect”
别名概述 PHP5.3+支持命名空间:namespace,命名空间的一个重要功能是能够使用别名(alias)来引用一个符合规则的名字. 命名空间支持3中形式的别名引用(或称之为引入)方式:类(clas ...
- android 虚拟按键是通过哪种机制上报的?
1.在normal mode下,tp button也是和其他触摸事件一样,以坐标形式的input_event进行上报.在初始化时会通过tpd_button_setting()函数依据定义在tpd_cu ...
- Nginx系列(三)--管理进程、多工作进程设计
Nginx由一个master进程和多个worker进程组成,但master进程或者worker进程中并不会再创建线程. 一.master进程和worker进程的作用 master进程 不须要处理网络事 ...
- 3d touch 的使用(一)
废话不多说,直接上代码------------------ 在 - (BOOL)application:(UIApplication *)application didFinishLaunchingW ...
- github结合TortoiseGit使用sshkey,无需每次输入账号和密码
首先需要明确,github上支持三种方式进行项目的clone https,ssh,subversion ssh的方式 git@github.com:用户名/版本库t.git ...
- php.ini配置文件参数优化
用于生产环境中的PHP需要对其进行优化,让PHP自身发挥更好的性能,除了写好PHP代码,还要配置好php-fpm以及php.ini调优.本文从内存.OPcache.上传.会话以及安全等方面讲解php. ...
- 源码编译Oprofile
上菜了翠花:首先编译Oprofile需要三个源码:binutils.popt与oprofile Linux版(由于是在64位的linux系统编译-----敬请参考“在64位linux上编译32位程序” ...