ACboy needs your help

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4912    Accepted Submission(s): 2651

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
 
Source
 
 
题目意思:
有n个课程,每个课程在不同天数得到不同价值,总共m天,问m天之内得到最大的价值。
 
思路:
每个课程若在i天完成,就不能在j天完成。以n个课程为组,天数为体积,价值为价值,即得到分组背包的模型。
 
dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[k]]+w[k])//dp[i][j]表示前i组已占用j体积时最大价值。
 
代码:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6. #include <queue>
  7. #include <cmath>
  8. #include <set>
  9. using namespace std;
  10.  
  11. #define N 105
  12.  
  13. int max(int x,int y){return x>y?x:y;}
  14. int min(int x,int y){return x<y?x:y;}
  15. int abs(int x,int y){return x<?-x:x;}
  16.  
  17. int a[N][N];
  18. int n, m;
  19. int dp[N];
  20.  
  21. main()
  22. {
  23. int i, j, k;
  24. while(scanf("%d %d",&n,&m)==){
  25. if(!n&&!m) break;
  26. for(i=;i<=n;i++){
  27. for(j=;j<=m;j++)
  28. scanf("%d",&a[i][j]);
  29. }
  30. memset(dp,,sizeof(dp));
  31. for(i=;i<=n;i++){
  32. for(j=m;j>=;j--){
  33. for(k=;k<=j;k++){
  34. dp[j]=max(dp[j],dp[j-k]+a[i][k]);
  35. }
  36. }
  37. }
  38. printf("%d\n",dp[m]);
  39. }
  40. }

HDU 1712 分组背包的更多相关文章

  1. hdu 1712 (分组背包入门)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 问题 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组, ...

  2. ACboy needs your help(HDU 1712 分组背包入门)

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

  3. HDU 3033 分组背包变形(每种至少一个)

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 4341 分组背包

    B - Gold miner Time Limit:2000MS      Memory Limit:32768KB     Description Homelesser likes playing ...

  5. HDU 3033 分组背包(至少选一个)

    分组背包(至少选一个) 我真的搞不懂为什么,所以现在就只能当作是模板来用吧 如果有大牛看见 希望评论告诉我 &代码: #include <cstdio> #include < ...

  6. HDU 3033 分组背包

    给出N个物品.M金钱.W种类 给出N个物品的性质:所属种类,花费.价值 求每一种类物品至少一个的前提下,所能购买到的最大价值 dp[i][k]表示在第i种物品.总花费为k的最大价值 dp[i][k]= ...

  7. 背包系列 hdu 3535 分组背包

    题意: 有n组工作,现在有T分钟时间去做一些工作.每组工作里有m个工作,并且类型为s,s类型可以为0,1,2,分别表示至少选择该组工作的一项,至多选择该工作的一项,不限制选择.每个工作有ci,gi两个 ...

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

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

  9. HDU - 1712 (分组背包模板)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意:给你n个课程,每个课程有很多种学习方法,用的时间和取得的效果都不一样,现在你只有m天时间用来学 ...

随机推荐

  1. CentOS系统启动流程

    CentOS系统启动流程 POST --> Boot Sequence(BIOS) --> Boot Loader(MBR) --> kernel(ramdisk) --> r ...

  2. [issue] [iOS 10] 升级后无法真机测试 Could not find Developer Disk Image

    说明:更新了手机的到了iOS 10.0.2.真机调试时候提示"Could not find Developer Disk Image" 并且之前就下载了Xcode8,但是没有安装X ...

  3. PHP: 深入pack/unpack

    https://my.oschina.net/goal/blog/195749 PHP作为一门为web而生的服务器端开发语言,被越来越多的公司所采用.其中不乏大公司,如腾迅.盛大.淘米.新浪等.在对性 ...

  4. 俄罗斯画师Mikhail Rakhmatullin作品

  5. T-SQL编程 —— 用户自定义函数(标量函数)

    用户自定义函数 在使用SQL server的时候,除了其内置的函数之外,还允许用户根据需要自己定义函数.根据用户定义函数返回值的类型,可以将用户定义的函数分为三个类别: 返回值为可更新表的函数 如果用 ...

  6. pinpoint 安装部署

    .markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(171, 178 ...

  7. maven多模块下使用JUnit进行单元测试

    1.选中需要进行测试的service类,右键->new->other->JUnit Test Case,如下图: 2.编写测试代码如下: AppServiceTest.java im ...

  8. Linux-设置固定IP

    第一步:激活网卡 系统装好后默认的网卡是eth0,用下面的命令将这块网卡激活. # ifconfig eth0 up 第二步:设置网卡进入系统时启动 想要每次开机就可以自动获取IP地址上网,就要设置网 ...

  9. Java知识积累2-StringReverse实现文字(单词)倒叙输出

    package String; import java.util.Stack;import java.util.StringTokenizer; public class StringReverse ...

  10. PostGIS导入导出SHP文件常用命令

    SHP导入POSTGIS数据库 引用 直接导入数据库 shp2pgsql  -I -s 2437 -W GBK shop_point.shp public.ntable | psql -U postg ...