hdu5188 加限制的01背包问题
http://acm.hdu.edu.cn/showproblem.php?
pid=5188
One day, zhx takes part in an contest. He found the contest very easy for him.
There are n problems
in the contest. He knows that he can solve the ith problem
in ti units
of time and he can get vi points.
As he is too powerful, the administrator is watching him. If he finishes the ith problem
before time li,
he will be considered to cheat.
zhx doesn't really want to solve all these boring problems. He only wants to get no less than w points.
You are supposed to tell him the minimal time he needs to spend while not being considered to cheat, or he is not able to get enough points.
Note that zhx can solve only one problem at the same time. And if he starts, he would keep working on it until it is solved. And then he submits his code in no time.
Seek EOF as
the end of the file.
For each test, there are two integers n and w separated
by a space. (1≤n≤30, 0≤w≤109)
Then come n lines which contain three integers ti,vi,li.
(1≤ti,li≤105,1≤vi≤109)
1 3
1 4 7
3 6
4 1 8
6 8 10
1 5 2
2 7
10 4 1
10 2 3
7
8
zhx is naive!
/**
hdu5188 有限制条件的01背包问题
题目大意:有n道题i题用时ti秒,得分vi,在li时间点之前不能做出来,并且一道题不能分开几次做(一旦開始做,必须在ti时间内把它做完)
问得到w分的最小用时是多少
解题思路:非常像01背包的基本题,可是有一个li分钟前不能AC的限制,因此第i道题必须在最早第(li-ti)时刻做,我们依照l-t递增排序,然后依照经典解法来做
即可了
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std; struct note
{
int t,v,l;
bool operator <(const note &other)const
{
return l-t<other.l-other.t;
} }node[35]; int n,m;
int dp[3000005]; int main()
{
while(~scanf("%d%d",&n,&m))
{
int sum=0,ans=0,up=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&node[i].t,&node[i].v,&node[i].l);
sum+=node[i].v;
ans+=node[i].t;
up=max(up,node[i].l);
}
if(m>sum)
{
printf("zhx is naive!\n");
continue;
}
sort(node,node+n);
up=max(up,ans);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
for(int j=up;j>=node[i].l;j--)
{
if(j>=node[i].t)
{
dp[j]=max(dp[j],dp[j-node[i].t]+node[i].v);
}
}
}
int flag=0;
for(int i=0;i<=up;i++)
{
if(dp[i]>=m)
{
printf("%d\n",i);
flag=1;
break;
}
}
if(flag==0)
{
printf("zhx is naive!\n");
}
}
return 0;
}
One day, zhx takes part in an contest. He found the contest very easy for him.
There are n problems
in the contest. He knows that he can solve the ith problem
in ti units
of time and he can get vi points.
As he is too powerful, the administrator is watching him. If he finishes the ith problem
before time li,
he will be considered to cheat.
zhx doesn't really want to solve all these boring problems. He only wants to get no less than w points.
You are supposed to tell him the minimal time he needs to spend while not being considered to cheat, or he is not able to get enough points.
Note that zhx can solve only one problem at the same time. And if he starts, he would keep working on it until it is solved. And then he submits his code in no time.
Seek EOF as
the end of the file.
For each test, there are two integers n and w separated
by a space. (1≤n≤30, 0≤w≤109)
Then come n lines which contain three integers ti,vi,li.
(1≤ti,li≤105,1≤vi≤109)
1 3
1 4 7
3 6
4 1 8
6 8 10
1 5 2
2 7
10 4 1
10 2 3
7
8
zhx is naive!
hdu5188 加限制的01背包问题的更多相关文章
- 01背包问题(动态规划)python实现
01背包问题(动态规划)python实现 在01背包问题中,在选择是否要把一个物品加到背包中.必须把该物品加进去的子问题的解与不取该物品的子问题的解进行比較,这样的方式形成的问题导致了很多重叠子问题, ...
- 算法笔记(c++)--01背包问题
算法笔记(c++)--经典01背包问题 算法解释起来太抽象了.也不是很好理解,最好的办法就是一步步写出来. 背包问题的核心在于m[i][j]=max(m[i-1][j],m[i-1][j-w[i]]+ ...
- PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***
1068 Find More Coins (30 分) Eva loves to collect coins from all over the universe, including some ...
- 0-1背包问题——回溯法求解【Python】
回溯法求解0-1背包问题: 问题:背包大小 w,物品个数 n,每个物品的重量与价值分别对应 w[i] 与 v[i],求放入背包中物品的总价值最大. 回溯法核心:能进则进,进不了则换,换不了则退.(按照 ...
- 201871030138-杨蕊媛 实验二 个人项目—《D{0-1}背包问题》项目报告
项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST 这个作业要求链接 https://www.cnblogs.com/nwnu-dai ...
- 01背包问题:POJ3624
背包问题是动态规划中的经典问题,而01背包问题是最基本的背包问题,也是最需要深刻理解的,否则何谈复杂的背包问题. POJ3624是一道纯粹的01背包问题,在此,加入新的要求:输出放入物品的方案. 我们 ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- HDU 1864最大报销额 01背包问题
B - 最大报销额 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- HDOJ 2546饭卡(01背包问题)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如 ...
随机推荐
- C - 字符识别?
C - 字符识别? Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Sta ...
- 解决ListView 和ScroolView 共存 listItem.measure(0, 0) 空指针
在网上找到ListView 和ScroolView 共存的方法无非是给他每个listview 重新增加高度,但是android 的设计者始终认为这并不是一种好的实现方法.但是有的时候有必须要用这种蛋疼 ...
- codeforces 245H . Queries for Number of Palindromes 区间dp
题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...
- 树莓派高级GPIO库,wiringpi2 for python使用笔记(三)GPIO操作
GPIO库的核心功能,当然就是操作GPIO了,GPIO就是"通用输入/输出"接口,比如点亮一个LED.继电器等,或者通过iic spi 1-wire等协议,读取.写入数据,这都是G ...
- 树莓派读取DHT11传感器的源代码
import wiringpi2 as gpio owpin=8 #第8脚为1-wire脚 def getval(owpin): tl=[] #存放每个数据位的时间 tb=[] #存放数据位 gpio ...
- 【原创】JPEG图像密写研究(二) 哈夫曼树的建立
[原创]记录自己研究的过程,仅供参考,欢迎讨论... 在根据JPEG图像文件结构读取完文件后,提取出其中DHT段,利用其中内容建立哈夫曼树,便于之后译码工作.这里需要注意的是文件中的哈夫曼表数量不固定 ...
- xmu1214: 购物
1214: 购物 Time Limit: 500 MS Memory Limit: 64 MBSubmit: 10 Solved: 5[Submit][Status][Web Board] Des ...
- svn中的Trunk,branches,tags深度理解
trunk.就是主干,这个目录以下直接放源代码了,我们创建项目的时候,把项目源代码放到这个目录.import进svn branches.就是分支,以下可能有非常多trunk,比方trunk_1_0_1 ...
- Css Rest 方法
在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是 重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈现相同的 ...
- KeCode对照表(键盘按键的获取)
KeyCode键盘对照表: http://www.cnblogs.com/furenjian/articles/2957770.html