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 问在满足条件的情况下 ...
随机推荐
- 如何制作一个类似Tiny Wings的游戏(2) Cocos2d-x 2.1.4
在第二篇<如何制作一个类似Tiny Wings的游戏>基础上,增加添加主角,并且使用Box2D来模拟主角移动,原文<How To Create A Game Like Tiny Wi ...
- Android入门第十六篇之Style与Theme [转]
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验,这些客户端都做得布局合理而且美观.. ...
- openstack之nova启动实例过程
概述: 启动一个实例包含以下步骤: API server:处理用户请求并转发至cloud controller cloud controller:处理计算节点.网络控制.api server 以及sc ...
- 如何在其他类中实现继承自CFormView类的对象
今天项目开发中,我们创建了一个对话框资源,并创建了一个派生自CFormView的类(假设为CMyClassDlg)来管理它. CMyClassDlg.h #pragma once // CMyClas ...
- XML编程与应用-读取XML
实例:使用XmlTextReader类的对象读取XML文档 代码如下 using System; using System.Collections.Generic; using System.Linq ...
- CheckBox只选择一项
最近做一个问卷的页面,客户那边说要使用checkbox而且只能选择一项 就写了下面的代码 <html xmlns="http://www.w3.org/1999/xhtml" ...
- validate 表单验证
转自博客园:http://www.cnblogs.com/easyinsc/archive/2009/02/27/1407826.html (1)required:true ...
- “Zhuang.Data”轻型数据库访问框架(二)框架的入口DbAccessor对象
目录: “Zhuang.Data”轻型数据库访问框架(一)开篇介绍 “Zhuang.Data”轻型数据库访问框架(二)框架的入口DbAccessor对象 先来看一段代码 DbAccessor dba ...
- SQLite 字符串连接
对Mysql可以使用CONCAT进行字符串连接, 但使用sqlite时,没有找到相应的方法,后在网上查找后,可以使用||来连接字符串 例: select 'a'||'b'
- javascript小练习—函数接收参数并弹出
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...