hdu 1712 ACboy needs your help(分组背包)
来源:互联网 发布:firefox for linux 编辑:程序博客网 时间:2024/05/16 04:40
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
这是一道分组背包的题目,之前看树形dp结果有用到分组背包发现自己其实并不会……先来补一下背包吧。
分组背包即一个容量为v的背包,每个组中只能选取一件或不选,第i件物品的价值为w[i],大小为c[i],问如何取使得背包总价值最大。
设dp[x]为前i件物品在容量为x的时候最大价值。第一重循环将i从1到n遍历,也就是将一组组物品加进去的情况。
而接下来的递推关系则可以是在固定某一容量的时候,依次尝试将这组中的每件物品放入。
for(int v=V;v>=0;v--) //背包容量减少 for(int k=1;k<=m;k++) //组别中不同的物品,注意v-c[k]>=0 dp[v]=max(dp[v],dp[v-c[i]]+w[i]);
在这种情况下,背包容量是递减的,在某一个容量时尝试了放入这组中任意一个物品的情况,并取得了最优解,而这个过程中最终改变的只有容量v的情况,在改变的过程中用到的只有可能是容量小于v的值,此时可以保证无论放入哪个物品,背包中都没有同组别的物品,因为背包容量小于v的情况还并未被放入。
所以这种情况下可以保证每组中至多有一个物品被选入。
另外一种可能会想到每组中物品固定时,背包容量不断减小,即
for(k=1;k<=m;k++) //不同费用的物品 for(v=V;v>=0;v--) //背包容量
但是这种情况下,某一个物品在被选入的时,dp[v]发生改变,此时背包中已经包含了此组别物品,而进行接下来的循环时,dp[v]的值进行更新是可能会用到之前已经改变过的值,而这种情况会造成同一组物品中可能会选取两件及以上,所以不可行。
以下是hdu1712的代码
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#define maxn 150using namespace std;int dp[maxn];int a[maxn][maxn];int m,n;void GroupPack(){ memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int v=m;v>=0;v--) for(int k=1;k<=v;k++) dp[v]=max(dp[v],dp[v-k]+a[i][k]); cout<<dp[m]<<endl;}int main(){ while(cin>>n>>m) { if(n==0&&m==0) break; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j]; GroupPack(); } return 0;}
- HDU 1712 ACboy needs your help(分组背包)
- 【hdu 1712】ACboy needs your help (分组背包)
- hdu 1712 ACboy needs your help(分组背包)
- hdu 1712 ACboy needs your help(分组背包)
- hdu 1712 ACboy needs your help(分组背包)
- HDU 1712 ACboy needs your help(DP 分组背包)
- hdu 1712 ACboy needs your help (分组背包)
- hdu 1712ACboy needs your help 01背包(分组)
- hdu 1712 ACboy needs your help (分组背包)
- hdu 1712 ACboy needs your help(分组背包入门)
- HDU 1712 - ACboy needs your help(分组背包)
- HDU 1712 ACboy needs your help(分组背包)
- HDU 1712 ACboy needs your help (分组背包问题)
- hdu 1712 ACboy needs your help(分组背包)
- hdu 1712 ACboy needs your help (分组背包)
- HDU 1712 ACboy needs your help (简单分组背包)
- HDU 1712 ACboy needs your help(分组背包)
- hdu 1712 ACboy needs your help(分组背包)
- leecode 解题总结:112. Path Sum
- STL Allocator
- 数据库备份的方式
- spring常用注解解释
- B1023. 组个最小数
- hdu 1712 ACboy needs your help(分组背包)
- 7月28日--JavaScript进阶篇2
- Python--数学运算--金融应用程序
- VS 2008 可扩展性开发(三):Add-In运行机制解析(上)
- Scala使用JUnit4单元测试
- Vuforia实现AR效果
- android studio 通过Terminal 查找MD5 SHA1
- 了解Mybatis
- PHP代码的执行的过程