HDU1712-ACboy needs your help

描述:

  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!=0 && m!=0 ){
        memset(value,0,sizeof(value));
        for( int i=1;i<=n;i++ )
            for( int j=1;j<=m;j++ )
                scanf("%d",&value[i][j]);
        memset(dp,0,sizeof(dp));//并未要求背包放满

        for( int i=1;i<=n;i++ ){
            for( int j=1;j<=m;j++ ){
                max=0;
                for( int k=0;k<=j;k++ )//第i个物品放0-j个
                    max=(max>dp[i-1][j-k]+value[i][k])?max:dp[i-1][j-k]+value[i][k];
                dp[i][j]=max;//背包容量为j,放前i个物品得到的最大值
            }
        }
        printf("%d\n",dp[n][m]);
    }
    system("pause");
    return 0;
}

  空间复杂度优化。可以看出,当计算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!=0 && m!=0 ){
        memset(value,0,sizeof(value));
        for( int i=1;i<=n;i++ )
            for( int j=1;j<=m;j++ )
                scanf("%d",&value[i][j]);
        memset(dp,0,sizeof(dp));//并未要求背包放满

        for( int i=1;i<=n;i++ ){
            for( int j=m;j>=1;j-- ){
                max=0;
                for( int k=0;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 0;
}
时间: 2024-11-06 21:20:47

HDU1712-ACboy needs your help的相关文章

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分组背包

题意:一共$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

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

【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(分组背包)

题目: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

题目链接: 点击打开链接 题目: ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3741    Accepted Submission(s): 1935 Problem Description ACboy has N courses this term, and he plans to sp

HDUOJ P1702 ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8599    Accepted Submission(s): 4306 Problem Description ACboy was kidnapped!! he miss his mother very much and is very scare now.You can't image

(hdu step 8.1.1)ACboy needs your help again!(STL中栈和队列的基本使用)

题目: ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 73 Accepted Submission(s): 57   Problem Description ACboy was kidnapped!! he miss his mother very much and is very

HDU 1702 ACboy needs your help again!(附加优先队列)

ACboy was kidnapped!! he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the ma