HDU3535-AreYouBusy
描述:
As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, because there are some other things for her to do, which makes her nearly mad.
What's more, her boss tells her that for some sets of duties, she must choose at least one job to do, but for some sets of things, she can only choose at most one to do, which is meaningless to the boss. And for others, she can do of her will. We just define the things that she can choose as "jobs". A job takes time , and gives xiaoA some points of happiness (which means that she is always willing to do the jobs).So can you choose the best sets of them to give her the maximum points of happiness and also to be a good junior(which means that she should follow the boss's advice)?
There are several test cases, each test case begins with two integers n and T (0<=n,T<=100) , n sets of jobs for you to choose and T minutes for her to do them. Follows are n sets of description, each of which starts with two integers m and s (0<m<=100), there are m jobs in this set , and the set type is s, (0 stands for the sets that should choose at least 1 job to do, 1 for the sets that should choose at most 1 , and 2 for the one you can choose freely).then m pairs of integers ci,gi follows (0<=ci,gi<=100), means the ith job cost ci minutes to finish and gi points of happiness can be gained by finishing it. One job can be done only once.
代码:
分组背包问题。组内有多种情况:至少选一个,至多选一个,无限制。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 110
#define MIN -1000000
int MAX(int a,int b,int c){
if( a<b ) a=b;
if( a<c ) a=c;
return a;
} int main(){
int n,t,val[N],time[N],dp[N][N];//dp第一维代表第n组物品;第二维代表背包容量,即时间
int m,s;
while( scanf("%d%d",&n,&t)!=EOF )
{
memset(dp,,sizeof(dp));//没有要求装满
for( int i=;i<=n;i++ )
{
scanf("%d%d",&m,&s);
for( int j=;j<=m;j++ )
scanf("%d%d",&time[j],&val[j]);
if( s== )//至少要选一个物品
{
for( int j=;j<=t;j++ )
dp[i][j]=MIN;//如果这一组的物品没有选,那么最后的解也将为负无穷,代表无解。
for( int j=;j<=m;j++ )//m个物品
{
for( int k=t;k>=time[j];k-- )//背包容量递减,k-time[j]也递减,这样保证数据不会重叠
{
//dp[i][k-time[j]]+val[j]:从这一组物品的上一个状态累加。组内决策
//dp[i-1][k-time[j]]+val[j]:直接从上一组物品累加,代表第一次加入该物品。从上一个分组开始决策
dp[i][k]=MAX(dp[i][k],dp[i][k-time[j]]+val[j],dp[i-][k-time[j]]+val[j]);
}
}
}
else if( s== )//最多选择一个
{
for( int j=;j<=t;j++ )
dp[i][j]=dp[i-][j];//可以不选择任何一个物品,最后的解即为上一组的解。
for( int j=;j<=m;j++ )
{
for( int k=t;k>=time[j];k-- )
dp[i][k]=max(dp[i][k],dp[i-][k-time[j]]+val[j]);//只能从上一个分组开始决策,代表最多选择一个
}
}
else if( s== ){//无限制
for( int j=;j<=t;j++ )
dp[i][j]=dp[i-][j];//可以不选择任何一个物品,最后的解即为上一组的解。
for( int j=;j<=m;j++ )
{
for( int k=t;k>=time[j];k-- )
{
//组内和组间决策
dp[i][k]=MAX(dp[i][k],dp[i][k-time[j]]+val[j],dp[i-][k-time[j]]+val[j]);
}
}
}
}
printf("%d\n",max(-,dp[n][t]));//无解
}
system("pause");
return ;
}
HDU3535-AreYouBusy的更多相关文章
- HDU3535——AreYouBusy
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题目意思:给出两个数n,T,分别表示有n个任务集合,T的总时间,对于每个任务集合有两个属性m和t ...
- HDU3535 AreYouBusy 混合背包
题目大意 给出几组物品的体积和价值,每组分为三种:0.组内物品至少选一个:1.组内物品最多选一个:2.组内物品任意选.给出背包容量,求所能得到的最大价值. 注意 仔细审题,把样例好好看完了再答题,否则 ...
- HDU 3535 AreYouBusy(混合背包)
HDU3535 AreYouBusy(混合背包) http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意: 给你n个工作集合,给你T的时间去做它们.给你m和 ...
- hdu3535(AreYouBusy)
题目链接:传送门 题目大意:有 n 组任务,m 个体力,每组任务有 k 个,分类为 f,每个任务花费 x 体力,得到 y 开心值,求最大开心值,若不能完成输出-1 分类为 0:这一组中的 k 个任务至 ...
- AreYouBusy
AreYouBusy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3535 AreYouBusy 分组背包
AreYouBusy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- HDU 3535 AreYouBusy 经典混合背包
AreYouBusy Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- AreYouBusy HDU - 3535 (dp)
AreYouBusy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu3535题解
hdu3535:http://acm.hdu.edu.cn/showproblem.php?pid=3535 该题是非常全面的一道分组背包问题.其实理解了最多一个的分组背包问题,解题起来也是很简单的. ...
- HDU 3535 AreYouBusy (混合背包)
题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...
随机推荐
- Linux编程---套接字
网络相关的东西差点儿都是建立在套接字之上.所以这个内容对于程序猿来说还是蛮重要的啊. 事实上套接字也就是一个特殊的设备文件而已,我始终不能明确为什么要叫套接字.这么个奇怪的名字.只是还是就这样算了吧. ...
- Android图片裁剪之自由裁剪
我的博客http://blog.csdn.net/dawn_moon 客户的需求都是非常怪的.我有时候在给客户做项目的时候就想骂客户是sb.可是请你相信我,等你有需求,自己变成客户的时候,给你做项目的 ...
- JavaScript引用类型之Array数组的拼接方法-concat()和截取方法-slice()
1.concat() 基于当前数组中的所有项创建一个新数组(也就是副本),然后将接收到的参数添加到副本的末尾,最后返回新构建的数组.也就是说,concat()在向数组中追加元素时,不会改变原有数组 ...
- Heritrix个性化设置抓取目标
本文是Heritrix的使用的高级篇,针对对Heritrix已经能够运行的码农朋友们! 我们在抓取网页的时候,网页的链接中往往会包含有js.css.图片.视频等文件,第一次执行抓取任务的时候,许多农民 ...
- Linux 文件内容转码
文件内容的转换: iconv -f GB2312 -t UTF-8 gb1.txt >gb2.txt-f, –from-code=名称 原始文本编码-t, –to-code=名称 输出编码-o, ...
- R与数据分析旧笔记(四)画地图练习
> library(maps) > library(geosphere) 载入需要的程辑包:sp > map("state")#画美国地图 > map(&q ...
- SQL1-(增删改查、常用函数)
USE flowershopdb --全球唯一标识符(GUID UUID) SELECT NEWID() --增删改查 --INSERT [INTO] <表名> [列名] VALUES & ...
- 【算法】计算一篇文章的单词数(C、Java语言实现)
1. C语言:一个字符一个字符的读取 (有空再贴出来) 2.Java语言:按行读取,并用正则分割成多个单词,再用MapReduce并行计算单词数 (我使用的是ieda,有些地方跟eclipse有点区别 ...
- 四轴飞行器1.1 Matlab 姿态显示
四轴飞行器1.1 Matlab 姿态显示 开始做四轴了,一步一步来,东西实在很多,比较杂.先做matlab上位机,主要用来做数据分析,等板子到了可以写飞控的程序了,从底层一层一层开始写..希望能好好的 ...
- Cortex-M3 动态加载二(RWPI数据无关实现)
上一篇关于动态加载讲述的是M3下面的ropi的实现细节,这一篇则讲述RW段的实现细节以及系统加载RW段的思路,我在M3上根据这个思路可以实现elf的动态加载,当然进一步的可以优化很多东西,还可以研究将 ...