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(分组背包模板)的更多相关文章

  1. hdu1712 ACboy needs your help 分组背包

    最基础的分组背包~ #include <iostream> #include <cstdio> #include <cstdlib> #include <cs ...

  2. [hdu1712]ACboy needs your help分组背包

    题意:一共$m$天,$n$门课程,每门课程花费$i$天得到$j$的价值,求最后获得的最大价值 解题关键:分组背包练习,注意循环的顺序不能颠倒 伪代码: $for$ 所有的组$k$   $for{\rm ...

  3. HDU - 1712 (分组背包模板)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意:给你n个课程,每个课程有很多种学习方法,用的时间和取得的效果都不一样,现在你只有m天时间用来学 ...

  4. 分组背包模板题 hdu1712

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 第一次接触分组背包,参考博客:https://blog.csdn.net/yu121380/ar ...

  5. HDU 1712 ACboy needs your help (分组背包模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...

  6. hdu 1712 ACboy needs your help 分组背包

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem ...

  7. ACboy needs your help-分组背包模板题

    id=17676" target="_blank" style="color:blue; text-decoration:none">ACboy ...

  8. D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题

    http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...

  9. dp之分组背包hdu3535(推荐)

    题意:有0,1,2三种任务,0任务中的任务至少得完成一件,1中的任务最多完成1件,2中的任务随便做.每一个任务最多只能做一次 .n代表有n组任务,t代表有t分钟,m代表这组任务有m个子任务,s代表这m ...

随机推荐

  1. Material Design系列第三篇——Using the Material Theme

    Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...

  2. 【Python3】 PyV8的安装与使用

    centos7  python3.6.4 1 安装boost  :  PyV8 依赖于Boost yum install boost yum install boost-devel yum insta ...

  3. 为什么WAN口IP和外网IP不一样(不一致)?

    正常的网络应该是动态公网ip,也就是路由器里面的WAN口IP与www.ip138.com上面显示的是一致的,不一致的话则说明该网络被电信或者联通做了NAT转发,导致您获取到了一个虚假的IP地址,无法用 ...

  4. SharpGL学习笔记(十) 常见的光源类型,创建光源

    在OpenGL中,使用光源的特性组合,如颜色,位置,方向等等,可以创建多种不同类型的灯光. 常见的几种灯光类型有: 定向光源(directonal) 定位光源(positional) 衰减光源 聚光灯 ...

  5. php 加密压缩

    php 把文件打成压缩包 ,可以去搜下 pclzip 搜很好多地方没有找到对压缩包进行加密操作的. 如果服务器是linux 那么见代码: $filename="test.csv"; ...

  6. SVN服务端安装

    1 首先安装SVN和Subversion. 安装文件可自行百度. 2 在服务端创建版本库. 我的安装目录是c:\Program Files(x86)\Subversion. 安装完成后在安装目录下sh ...

  7. httpWebRequest获取流和WebClient的文件抓取

    httpWebRequest获取流和WebClient的文件抓取 昨天写一个抓取,遇到了一个坑,就是在获取网络流的时候,人为的使用了stream.Length来获取流的长度,获取的时候会抛出错误,查了 ...

  8. Keepalived指定文件接收日志

    keepalived默认日志接收文件为/var/log/messages不方便查看,可以指定文件接收日志 修改配置文件/etc/sysconfig/keepalived KEEPALIVED_OPTI ...

  9. POJ2387-Till the cows come home【最短路】

    A - Til the Cows Come Home POJ - 2387 Bessie is out in the field and wants to get back to the barn t ...

  10. 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]

    题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...