hdu1712 分组背包 ACboy needs your help

ACboy needs your help

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

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

HDU 2007-Spring Programming Contest

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[200][200];
int f[1000];
int main(){
   int n,m;
   while(scanf("%d%d",&n,&m)!=EOF){
       if(n==0&&m==0)
       break;
    memset(f,0,sizeof(f));
       memset(a,0,sizeof(a));
       for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
            scanf("%d",&a[i][j]);
       }

       for(int i=1;i<=n;i++){
          for(int j=m;j>=0;j--){
            for(int k=1;k<=j;k++)
                f[j]=max(f[j],f[j-k]+a[i][k]);
          }
       }
       printf("%d\n",f[m]);
   }
   return 0;
}
时间: 2024-08-24 12:43:51

hdu1712 分组背包 ACboy needs your help的相关文章

hdu1712 分组背包

ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5964    Accepted Submission(s): 3251 Problem Description ACboy has N courses this term, and he plans to spend at most M days

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

http://acm.hdu.edu.cn/showproblem.php?pid=1712 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 arrang

【HDU1712】ACboy needs your help(分组背包)

将背包九讲往后看了看,学习了一下分组背包.来做几道入门题,试试手. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 #inc

[hdu1712]ACboy needs your help分组背包

题意:一共$m$天,$n$门课程,每门课程花费$i$天得到$j$的价值,求最后获得的最大价值 解题关键:分组背包练习,注意循环的顺序不能颠倒 伪代码: $for$ 所有的组$k$   $for{\rm{ }}v = V..0$        $for$ 所有的$i$属于组$k$           $f[v] = \max (f[v],f[v] - c[i] + w[i])$ 背包问题本质上就是一种线性规划问题. 1 #include<bits/stdc++.h> 2 using namesp

HDU1712 ACboy needs your help(分组背包)

题目大意: 一个人在M天中完成N门课程,每门课程的分数和所用的时间有关系,求解如何安排学习得分最高. 输入:两个整数N和M,接下来是使一个N*M的矩阵A.A[i][j]代表用j天学习第i门课程的分数. 输出:得到的最大分数. 解题思路: 每门作业i只能选择一个对应的天数来完成,也就是矩阵的每一行中至多之能选择一个数,典型的分组背包问题: 分组背包: 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组,每组中的物品互相冲突,最多选一件.求解将哪些物品

HDU1712:ACboy needs your help(分组背包)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1712 解释看这里:http://www.cnblogs.com/zhangmingcheng/p/3940332.html 这题之前竟然做过,竟然不记得了,做了一个小时,竟然没A,(我连分组背包干什么的都忘了) 这题的博客以前也写过,我重新写这篇博客,就是提醒自己一下. 代码: #include <iostream> using namespace std; int a[101][101],f[101

【hdu1712】分组背包(每组最多选1个)

[分组背包] [题意]ACboy要开始选课了,上一门课能够获得的收益和他上这门课的时间是有关的,然后给你若干门课,让你帮他进行选课,每一门课自然是只能选择一个课程时长,问你如何选择,才能使ACboy获得的受益最大. for(k=1;k<=K;k++) for(int v=V;v>=0;v--) for(int i=item in group k) f[v]=max(f[v],f[v-c[i]+w[i]);保证用到的都是k-1所更新的而不是k所更新的. 1 #include<cstdio&

hdu 1712 ACboy needs your help(分组背包入门)

1 /********************************************************** 2 题目: ACboy needs your help 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=1712 4 题意: 一开始输入n和m,n代表有n门课,m代表你有m天,然 5 后给你一个数组,val[i][j],代表第i门课,在通过j 6 天去修,会得到的分数.求在m天能得到的最大分数. 7 算法: 分组背包 8 9

HDU 1712 ACboy needs your help-dp-(分组背包模型)

题意:n门课程用m天来学习,每门课用不同的天数来学习会有不同的学分,求能得到的最大的学分 分析:第一次接触分组背包.分组背包的模型就是不同的物品有不同的花费和价值,求在规定花费内能得到的最大的价值,这前面跟以前的背包最大的不同是物品分为几组,每组内的物品最多只能选一种:dp[i][j]表示前i组花费j能得到的最大的价值,不过实际在做的时候用一维数组就可以了 公式: for 组i for 花费j (从大到小) for 组内物品k if(j>=c[k]) dp[j]=max(dp[j],dp[j-c