动态规划:HDU1712-ACboy needs your help(分组背包问题)
来源:互联网 发布:最新短期网络投资理财 编辑:程序博客网 时间:2024/05/22 13:19
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 21 21 32 22 12 12 33 2 13 2 10 0
Sample Output
346
解题心得:
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]); }}
阅读全文
0 0
- 动态规划:HDU1712-ACboy needs your help(分组背包问题)
- hdu1712 ACboy needs your help (分组背包)
- 分组背包 hdu1712 ACboy needs your help
- hdu1712 ACboy needs your help(分组背包)
- 背包(3)Hdu1712 ACboy needs your help(分组背包)
- (分组背包)ACboy needs your help(hdu1712)
- 简单分组背包ACboy needs your help(hdu1712)
- hdu1712---ACboy needs your help(dp,分组背包)
- HDU1712 ACboy needs your help(分组背包)
- hdu1712 ACboy needs your help(分组背包)
- HDU1712 ACboy needs your help(分组背包)
- hdu1712 ACboy needs your help(分组背包板子)
- hdu1712 ACboy needs your help 分组背包
- HdU1712 ACboy needs your help 01分组背包
- 【hdu1712】ACboy needs your help——分组背包
- ACboy needs your help(分组背包)
- ACboy needs your help(分组背包)
- HDU 1712 ACboy needs your help (分组背包问题)
- Visual Studio与SQL Server各版本密钥大全
- 初识hadoop
- [python] os.getcwd及os.path模块
- 命令行下编译java程序,出现错误:编码GBK的不可映射字符
- linux下动态库的符号冲突、隐藏和强制优先使用库内符号
- 动态规划:HDU1712-ACboy needs your help(分组背包问题)
- SSL/TLS协议运行机制的概述
- 空间射线在普吕克坐标系中的表示
- Finding Bovine Roots
- 在507干活的第2天
- 数据库完整性
- Splay树简单操作
- linux 将权限赋给某个组 chown
- 第15章 使用远程服务