ACboy needs your help

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 1
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], (1<=i<=N<=100,1<=j<=M<=100).A[i][j]
indicates if ACboy spend j days on ith course he will get profit of value A[i][j]. N = 0 and M = 0 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

2 2
1 2
1 3
2 2
2 1
2 1
2 3
3 2 1
3 2 1
0 0
 

Sample Output

3
4
6
 


解题心得:
1、这还是第一次看分组背包问题,分组背包问题的一个要求就是,在很多组中,每次只能从一个组中选取一个数,然后从多组中得到最优解。这就和0-1背包问题、多重背包和完全背包问题不同了。三种基础的背包问题(0-1、多重、完全)其实可以看成一个组,是单组之中随便选取,求最优解。所以在遇到多组背包问题的时候要能够区分的出来。

2、因为多组背包问题相对于三种基础的背包问题来说多了一个条件——组数。所以要加一层循环,组数。
公式
for 所有的组k
for v = V...0
for 所有i组的k
f[v]=max{f[v],f[v-c[i]]+w[i]}

要注意一点,三层循环之中容量必须放在第二层之中,否则就变成了0-1背包问题了

/*
之前在做这个题的时候看了一个人的博客,他把公式给写反了,v...V写到内层去了,
弄得一直WA,也是无语了,在这个博客里面纠正一下这个错误
*/ #include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
const int maxn2 = 1e2+10;
int maps[maxn2][maxn2];
int dp[maxn];
struct NUM
{
int va,cost;
}num[maxn];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m) && n+m)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&maps[i][j]); for(int i=1;i<=n;i++)
for(int j=m;j>=1;j--)
for(int k=1;k<=j;k++)
dp[j] = max(dp[j],dp[j-k]+maps[i][k]);
printf("%d\n",dp[m]);
}
}



动态规划:HDU1712-ACboy needs your help(分组背包问题)的更多相关文章

  1. HDU1712:ACboy needs your help(分组背包模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem Description ACboy has N courses this term, an ...

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

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

  3. hdu1712 ACboy needs your help 分组背包

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

  4. P1757 通天之分组背包 / hdu1712 ACboy needs your help (分组背包入门)

    P1757 通天之分组背包 hdu1712 ACboy needs your help hdu1712题意:A[i][j]表示用j天学习第i个课程能够得到A[i][j]的收益,求m天内获得的收益最大值 ...

  5. ACboy needs your help hdu 分组背包问题

    Description ACboy has N courses this term, and he plans to spend at most M days on study.Of course,t ...

  6. 分组背包----HDU1712 ACboy needs your help

    很简单的一道分组背包入门问题.不多解释了. #include <iostream> #include <cstdio> #include <cstring> usi ...

  7. HDU1712 ACboy needs your help(分组背包)

    每种课程学习不同天数可以获得不同价值,这可以看成一个组,那么题目就是分组背包的模板题了. 1 #include<cstdio> 2 #include<cstring> 3 #i ...

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

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

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

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

随机推荐

  1. Poj 1743——Musical Theme——————【后缀数组,求最长不重叠重复子串长度】

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22499   Accepted: 7679 De ...

  2. Ashx增删改查_动软

    1.首先展示列表 ashx 讲究的是个替换 这些就是属于ashx麻烦的地方 public void ProcessRequest(HttpContext context) { context.Resp ...

  3. Node.js连接MongoDB

    使用monk访问mongodb mongodb.monk都安装了依赖的前提下: 首先启动MongoDB 服务:mongod: 进入了mongodb后台管理,再通过终端创建数据库:use monk-ap ...

  4. MvvmCross框架在XamarinForms中的使用入门

    做XamarinForms快一年了,最近趁着项目不是很紧,有点空闲的时间,研究了一下MvvmCross这个框架,感觉挺高大上的.一边研究一下写点入门的东西吧,大部分的东西github都有. 1添加Pa ...

  5. 景安快云VPS挂载数据盘至指定目录 使得系统与数据分离

    如果我们细心的用户会发现购买景安快云VPS主机后,通过df检测看到系统盘大小与我们购买时候给的不一样,这个是很正常的事情.一般VPS主机商会通过给予系统盘和数据盘一并的数据磁盘给我们,但是默认我们看到 ...

  6. pyinstaller打包python源程序访问hive

    1.需求 使用hvie server一段时间后,业务部门需要自己不定时的查询业务数据,之前这一块都是他们提需求我们来做,后来发现这样重复一样的工作放在我们这边做是在没有效率,遂提出给他们工具或者web ...

  7. 测试发布(maven-assembly-plugin看好你哦)

    项目改成了maven管理,现场需要用增量补丁包的形式发布代码: 2015/4/21 以前试过用ant打补丁包,现在试试maven能不能做同样的事情: maven-assembly-plugin看着可以 ...

  8. Linux下bash的快捷键

    Ctrl + A  :切换到命令行开始 Ctrl + E :切换到命令行末尾 Ctrl + L : 清屏,相当于clear Ctrl + U :清除剪切光标前的内容 Ctrl + K :剪切清除光标后 ...

  9. 【BZOJ2427】[HAOI2010] 软件安装(缩点+树形DP)

    点此看题面 大致题意: 有\(N\)个软件,每个软件有至多一个依赖以及一个所占空间大小\(W_i\),只有当一个软件的直接依赖和所有的间接依赖都安装了,它才能正常工作并造成\(V_i\)的价值.求在容 ...

  10. 2018.7.2 如何用js实现点击图片切换为另一图片,再次点击恢复到原图片

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...