描述:

  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?

  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.

  For each data set, your program should output a line which contains the number of the max profit ACboy will gain.

代码:

  多重背包问题,不要求装满背包。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define MAX 105 int main(){
int n,m,dp[MAX][MAX],value[MAX][MAX],max;
while( scanf("%d%d",&n,&m)!=EOF && n!= && m!= ){
memset(value,,sizeof(value));
for( int i=;i<=n;i++ )
for( int j=;j<=m;j++ )
scanf("%d",&value[i][j]);
memset(dp,,sizeof(dp));//并未要求背包放满 for( int i=;i<=n;i++ ){
for( int j=;j<=m;j++ ){
max=;
for( int k=;k<=j;k++ )//第i个物品放0-j个
max=(max>dp[i-][j-k]+value[i][k])?max:dp[i-][j-k]+value[i][k];
dp[i][j]=max;//背包容量为j,放前i个物品得到的最大值
}
}
printf("%d\n",dp[n][m]);
}
system("pause");
return ;
}

  空间复杂度优化。可以看出,当计算dp[i][j]时,用到的数值为dp[i-1][0]到dp[i-1][j]的值,故背包容量的遍历顺序需反序,可以将dp二维数组优化为一维。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define MAX 105 int main(){
int n,m,dp[MAX],value[MAX][MAX],max;
while( scanf("%d%d",&n,&m)!=EOF && n!= && m!= ){
memset(value,,sizeof(value));
for( int i=;i<=n;i++ )
for( int j=;j<=m;j++ )
scanf("%d",&value[i][j]);
memset(dp,,sizeof(dp));//并未要求背包放满 for( int i=;i<=n;i++ ){
for( int j=m;j>=;j-- ){
max=;
for( int k=;k<=j;k++ )//第i个物品放0-j个
max=(max>dp[j-k]+value[i][k])?max:dp[j-k]+value[i][k];
dp[j]=max;//背包容量为j,放前i个物品得到的最大值
}
}
printf("%d\n",dp[m]);
}
system("pause");
return ;
}

HDU1712-ACboy needs your help的更多相关文章

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

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

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

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

  3. hdu1712 ACboy needs your help 分组背包

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

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

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

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

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

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

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

  7. HZNU-ACM寒假集训Day7小结 背包DP

    背包问题 01背包 状态:f(i,j) 表示只能装前i个物品的情况下,容量为j的背包所能达到的最大总价值 状态转移方程:  f(i,j)=max(f(i-1,j),f(i-1,j-w[i])+v[i] ...

  8. 【泛化物品】【HDU1712】【ACboy needs your help】

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. hdu1712 分组背包 ACboy needs your help

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  10. 简单分组背包ACboy needs your help(hdu1712)

    题意:有n个任务,完成期限是m天,a[i][j]代表第i个任务用j天完成可以获得的利益,问在这m天里面可以获得的最大利益,每次只能做一个任务,即多个任务不能同时做; 分析;用dp[i][j]代表在做第 ...

随机推荐

  1. linux内核源码阅读之facebook硬盘加速flashcache之八

    前面我们的分析中重点关注正常的数据流程,这一小节关注如果有异常,那么流程是怎么走完的呢? 1)创建新任务时kcached_job申请不到 2)读写命中时cache块为忙 3)系统关机时处理,系统开机时 ...

  2. python-操作exel(xlrd,xlwt)

    1.使用第三方库 python中处理excel表格,常用的库有xlrd(读excel)表.xlwt(写excel)表.openpyxl(可读写excel表)等. xlrd读数据较大的excel表时效率 ...

  3. c#读取xml文件配置文件Winform及WebForm-Demo具体解释

    我这里用Winform和WebForm两种为例说明怎样操作xml文档来作为配置文件进行读取操作. 1.新建一个类,命名为"SystemConfig.cs".代码例如以下: < ...

  4. vs 2005 在IE下断点不起作用

    vs2005 加断点调试,ie下不起作用. 1. 点击[开始]->[运行] 命令:regedit. 2. 定位到HKEY_LOCALMACHINE -> SOFTWARE -> Mi ...

  5. SqlServer数据类型、C#SqlDbType对应关系及转换

    { { } } { SqlDbType dbType = SqlDbType.Variant; { dbType = SqlDbType.Int; dbType = SqlDbType.VarChar ...

  6. button变成href (即按钮超链效果)

    法一:   这种方法适合做单纯的HTML静态页面,因为它只有button的显示效果,但不能真的跳转.貌似是鸡肋,没多大用. 法二: 1.新打开一个页面 2.本页打开 在超链中实现打开新页面用targe ...

  7. shell检测interface是否已分配ip,qt调用shell脚本

    #include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...

  8. java——String的那边破事

    经典的先看下面一段代码,请问最终创建几个对象,分别在哪里? String s0 = new String("luoliang.me"); String s1 = "luo ...

  9. 一年开发ASP.NET MVC4项目经验总结

    一年开发ASP.NET MVC4项目所用所学技术经验总结 阅读目录 文章背景 前端所用技术摘要 后端所用技术摘要 1. 文章背景 本人2014年从Java转行到C#从事BS项目的开发,刚开始接触的是A ...

  10. C# WINFORM 线程中更新UI

    幸好今天是周末,有时间把这个问题记录一下.在多种语言之间切换,发现开发效率降的很低了,开发成本都集中到调式上了,C/C++这些放弃很久了,突然感觉线程这个问题搞的有点烦躁 我这里提到的线程中更新UI, ...