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 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如 ...
随机推荐
- Lua for Windows入门01
由于项目紧急,我都没来得及研究lua的基本知识就直接持枪上阵了.在实施编写的过程中,却次发现编程语言如此之美,第一次. 随着Lua+for+Windows+5.1.4-45版本的完全安装,最后跳出了一 ...
- 学习笔记--C#特性Attribute(一)
这个框框好烦人啊,删不掉 一.背景 [serializable] public class Person(){} 这是我第一次看到特性(Attribute),那时我还不知道这是什么,怎么会有这种写法, ...
- latex如何把目录页的页码去掉?
页眉的显示与关闭,清空,还有样式之间的切换,需要用到如下几个命令: \pagestyle 用于设置当前页以及后续页面的页眉显示情况(可称为页版式).中间页版式可由\thispagestyle命令来指 ...
- MySQL 数据显示宽度
例子: 把int 的显示宽度设置为 3 create table t(x int(3) zerofill); insert into t(x) values(1); select x from t; ...
- RTTI-CLASS
package com.xt.test; interface Test1Interface { } interface Test2Interface { } class Test1 implement ...
- 使用Fiddler解析WCF RIA Service传输的数据
原文 http://www.cnblogs.com/wintersun/archive/2011/01/05/1926386.html 使用Fiddler 2 解析WCF RIA Service传输的 ...
- 脑波设备mindwave二次开发框架
神念科技提供的mindwave提供了脑波耳机和相应的游戏,这些游戏你可以通过购买神念科技的mindwave耳机来获取,这里不多作介绍. 我们作为程序员,如果有了相应的创意,也可以通过他们提供的二次开发 ...
- gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典
gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典 gravitas
- Android中各种Adapter的使用方法
1.概念 Adapter是连接后端数据和前端显示的适配器接口.是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都须要用到Adapter.例如以下 ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...