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 问在满足条件的情况下 ...
随机推荐
- 奇怪的问题:android:focusable和android:clickable造成ListView的点击不了
今天花了我很长时间,才解决一个很奇怪的问题,就是在ListView的点击反应不了的问题…… 在ListView中,如果其中一个元素设置为android:focusable="true&quo ...
- [LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- date tod = boost::gregorian::day_clock::local_day(); //当前日期
boost 时间和日期 - 苦逼码农 - 博客频道 - CSDN.NET date tod = boost::gregorian::day_clock::local_day(); //当前日期
- E=MC2 - 搜搜百科
E=MC2 - 搜搜百科 1 E=MC2 质能等价理论是爱因斯坦狭义相对论的最重要的推论,即著名的方程式E=mC^2,式中E为能量,m为质量,C为光速:也就是说,一切物质都潜藏着质量乘于光速平方的能量 ...
- linux环境之监听端口配置
export JAVA_OPTS="-Dcom.sun.management.jmxremote.port=18950 -Dcom.sun.management.jmxremote.auth ...
- saiku导出excel单元格格式与中文列宽自动适应
在saiku导出excel后打开发现单元格的整数也显示为小数,并且含有中文的列宽没有自动适应,解决办法如下: 打开ExcelWorksheetBuilder.java文件,找到applyCellFor ...
- 怎样在一个页面使多个setInterval函数正常执行
var firstInterval; var secondInterval; function firstAlert(){ if(firstInterval) clearInterval(firstI ...
- ovs 实用案例
建立gre,xvlan:http://networkstatic.net/configuring-vxlan-and-gre-tunnels-on-openvswitch/ vm之间通过gre通信:h ...
- HTML之学习笔记(四)格式化标签和特殊字符
html常用的格式化标签使用如下 <html> <head> <title></title> </head> <body > & ...
- JavaScript之<script>标签简介
向html页面中插入JavaScrpt的主要方法,就是使用<script>元素,下面是Html 4.01为<script>定义的6个属性. 1.async:可选表示应该立即下载 ...