HDU1712:ACboy needs your help(分组背包模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1712
Problem Description
ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit? Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.
Next follow a matrix A[i][j], (<=i<=N<=,<=j<=M<=).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].
N = and M = ends the input. Output
For each data set, your program should output a line which contains the number of the max profit ACboy will gain. Sample Input Sample Output
分组背包
问题
有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。这些物品被划分为若干组,每组中的物品互相冲突,最多选一件。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。
算法
这个问题变成了每组物品有若干种策略:是选择本组的某一件,还是一件都不选。也就是说设f[k][v]表示前k组物品花费费用v能取得的最大权值,则有:
f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]|物品i属于组k}
使用一维数组的伪代码如下:
for 所有的组k
for v=V..0
for 所有的i属于组k
f[v]=max{f[v],f[v-c[i]]+w[i]}
注意这里的三层循环的顺序,甚至在本文的第一个beta版中我自己都写错了。“for v=V..0”这一层循环必须在“for 所有的i属于组k”之外。这样才能保证每一组内的物品最多只有一个会被添加到背包中。
另外,显然可以对每组内的物品应用P02(完全背包问题)中“一个简单有效的优化”。
小结
分组的背包问题将彼此互斥的若干物品称为一个组,这建立了一个很好的模型。不少背包问题的变形都可以转化为分组的背包问题(例如P07(有依赖的背包问题)),由分组的背包问题进一步可定义“泛化物品”的概念,十分有利于解题。
这是我第一次接触分组背包。
此题属于典型的分组背包,每组至多一个背包。
看了背包9讲,直接用他上面所写的套上去就行了,三重循环记住每一重的意义~!!!
#include <iostream>
using namespace std;
int a[][],f[];
int main()
{
int n,m,i,j,k;
while(cin >> n >> m && (n != || m != ))
{
memset(f,,sizeof(f));
for(i = ; i <= n; i++)
for(j = ; j <= m; j++)
cin >> a[i][j];
for(i = ; i <= n; i++) //第一重循环:分组数
for(j = m; j >= ; j--) //第二重循环:容量体积
for(k = ; k <= j; k++) //第三重循环:属于i组的k
f[j] = max(f[j],f[j-k]+a[i][k]);
cout << f[m] << endl;
}
return ;
}
HDU1712:ACboy needs your help(分组背包模板)的更多相关文章
- hdu1712 ACboy needs your help 分组背包
最基础的分组背包~ #include <iostream> #include <cstdio> #include <cstdlib> #include <cs ...
- [hdu1712]ACboy needs your help分组背包
题意:一共$m$天,$n$门课程,每门课程花费$i$天得到$j$的价值,求最后获得的最大价值 解题关键:分组背包练习,注意循环的顺序不能颠倒 伪代码: $for$ 所有的组$k$ $for{\rm ...
- HDU - 1712 (分组背包模板)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意:给你n个课程,每个课程有很多种学习方法,用的时间和取得的效果都不一样,现在你只有m天时间用来学 ...
- 分组背包模板题 hdu1712
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 第一次接触分组背包,参考博客:https://blog.csdn.net/yu121380/ar ...
- HDU 1712 ACboy needs your help (分组背包模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...
- hdu 1712 ACboy needs your help 分组背包
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem ...
- ACboy needs your help-分组背包模板题
id=17676" target="_blank" style="color:blue; text-decoration:none">ACboy ...
- D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题
http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...
- dp之分组背包hdu3535(推荐)
题意:有0,1,2三种任务,0任务中的任务至少得完成一件,1中的任务最多完成1件,2中的任务随便做.每一个任务最多只能做一次 .n代表有n组任务,t代表有t分钟,m代表这组任务有m个子任务,s代表这m ...
随机推荐
- Stratix内嵌存储器测试报告
Stratix和Stratix GX系列器件内嵌TriMatrix存储块包括512-bit M512块.4-Kbit M4K块及512-Kbit M-RAM块.TriMatrix存储结构可对 ...
- 【资源大全】.NET资源大全中文版(Awesome最新版)
算法与数据结构(Algorithms and Data structures) 应用程序接口(API) 应用程序框架(Application Frameworks) 模板引擎(Application ...
- 【cs229-Lecture13】高斯混合模型
本节内容: 1.混合高斯模型: 2.将混合高斯模型应用到混合贝叶斯模型:(应用:文本聚类) 3.结合EM算法,讨论因子分析算法: 4.高斯分布的有用性质. 混合高斯模型 将一般化的EM算法流程(下载笔 ...
- python基础---->python的使用(三)
今天是2017-05-03,这里记录一些python的基础使用方法.世上存在着不能流泪的悲哀,这种悲哀无法向人解释,即使解释人家也不会理解.它永远一成不变,如无风夜晚的雪花静静沉积在心底. Pytho ...
- jsp连接数据库的乱码问题 servlet请求参数编码处理get post
1.在所有需要读取数据的地方用下面的方式.同时jsp必须统一编码,如我都是UTF-8 String userName= new String(request.getParameter("us ...
- Android 1.6 PackageManagerService源码分析
文件清单 framework\base\services\core\java\com\android\server\pm\PackageManagerService.java PackageManag ...
- 开发常见错误之 :Missing artifact com.sun:tools:jar 1.7.0
Missing artifact com.sun:tools:jar 1.7.0 解决办法一: 手动配置pom.xml,添加一个dependency如下: <dependency> < ...
- Unity3D动作资源(AnimatinClip)优化
能做到去掉Scale曲线,降低浮点精度 using System; using UnityEngine; using System.Collections; using System.Collecti ...
- EJBCA的安装(基于Ubuntu 16.04 LTS + wildfly8 + ejbca6.3.11 + jdk7)
前一段时间折腾了一下PKI,用EJBCA在研究院内网搭建了一个CA,目前是提供给手机端(安卓和IOS)来和服务器端(nginx + Java应用)做安全连接的(客户端和服务器端双向认证) 由于EJBC ...
- CentOS 添加环境变量
1.修改环境变量需要修改/etc/profile export PATH="$PATH:/usr/src/ruby-1.9.3-p0/ruby:/usr/local/bin/gem&qu ...