描述:

  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. Matrix(多线程dp)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  2. 转载:做Java开发这一年 (火龙果软件)

    转载:http://www.uml.org.cn/success/201410205.asp 从去年到现在,从.NET转向Java开发(只是因为项目原因,绝对与平台好坏没有关系)差不多有一年的时间了. ...

  3. 将图片文件以byte的形式从导数据库中

    byte[] FileByteArray = new byte[FileLength];  //图象文件临时储存Byte数组                 //Stream StreamObject ...

  4. ORACLE恢复误删除的对象(表、存储过程等)

    1.恢复存储过程 原理就是利用了oracle里所有的存储过程的源代码都是存在dba_source里,而drop某个存储过程的时候,oracle这里肯定要去dba_source里把相关的源代码给dele ...

  5. RPC介绍以及编程

    1 RPC介绍 RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协 议. RPC采用客 ...

  6. C# 实现磁性窗体

    可以实现窗体的 吸附 移动 分离     using System; using System.Drawing; using System.Collections.Generic; using Sys ...

  7. Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇

    懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 ) 写下这些东西的同时 旁边放了两部电影 周星驰的<还魂夜> 甄子丹的<特殊身份& ...

  8. 解决一个Android Studio gradle的小问题

    自从Android Studio有了gradle之后,就经常有问题,最近在Ubuntu上用Android Studio的时候就遇到一个问题,每次项目目录更改了,Import项目,打开项目,还是新建项目 ...

  9. mysql 字段注释

    create table student3(id int(30) primary key comment 'ID',name varchar(255) comment '姓名',address var ...

  10. break的使用例一

    /* Name:break的使用例一 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月21日 02:28:24 Description:本程序代码无如何含 ...