hdu 1712, multiple-choice knapsack,

reference:

6.4 knapsack in Algorithms(算法概论), Sanjoy Dasgupta University of California, San Diego Christos Papadimitriou University of California at Berkeley Umesh Vazirani University of California at Berkeley

the unbounded knapsack and 0-1 knapsack are both illuminatingly discussed in the reference book, in chapter 6 dynamic programming, strongly recommended. the multiple-choice knapsack and bounded knapsack are variants of 0-1 knapsack.

//

#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAXSIZE 105
int dp[MAXSIZE]={0}, *p;
int profit[MAXSIZE];
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int n,m,i,j,k;
    while(scanf("%d%d",&n,&m)==2 && n>0 && m>0) {
        memset(dp+1,0,(m+1)*sizeof(dp[0]));
        for(i=0;i<n;++i) {
            for(j=1;j<=m;++j) scanf("%d",&profit[j]);
            /*
            // 多重背包, unbounded knapsack
            for(j=1;j<=m;++j) {
                for(k=1;k<=j;++k) {
                    dp[j]=std::max(dp[j],profit[k]+dp[j-k]);
                }
            }*/
            // 分组背包, multiple choice knapsack
            for(j=m, p=dp+m;p!=dp;--p, --j) {
                for(k=1;k<=j;++k) {
                    if(*p<profit[k]+p[-k])
                    *p=profit[k]+p[-k];
                }
            }
        }
        printf("%d\n",dp[m]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// ps. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

时间: 2024-08-29 19:52:51

hdu 1712, multiple-choice knapsack,的相关文章

hdu 1712 ACboy needs your help

转载请注明出处:http://blog.csdn.net/u012860063 题目链接: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 depen

HDU 1712 ACboy needs your help(DP)

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

HDU 1712 ACboy needs your help AC男需要你的帮助 (AC代码)分组的背包问题

分组的背包问题:有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组,每组中的物品互相冲突,最多选一件.求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. HDU 1712:AC男有m天时间可以上课,有n种课程可以选,但是每一种课程有各种的上课时长可选(因为并不是花的时间与收获就呈线性增长的,两者之间没什么对应关系,也许花1天收获2点经验,但是花2天就不是4点经验了,可能是3点而已).耗时与收获以矩阵的方式给出.(把同一课程

ACboy needs your help HDU - 1712

ACboy needs your help HDU - 1712 ans[i][j]表示前i门课共花j时间最大收益.对于第i门课,可以花k(0<=k<=j)时间,那么之前i-1门课共花j-k时间. 错误记录: 21行一个0写成1 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int a[101][101],ans[101][101];

hdu 1712 ACboy needs your help 裸的泛化物品

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 裸的泛化物品 泛化物品的意思就是物品的价值是变化的 取决于你给它多少花费 学习自背包九讲第八讲 dp[i][j]表示前i件物品共分配j花费的情况下能得到的最大值 三层循环 第一层是物品 第二层是枚举总花费 第三层是枚举最新的一层占总花费的数目 用滚动数组来实现 #include <cstdio> #include <cstdlib> #include <ctime>

DP HDU 1712

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

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

http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意: 有个人学习n门课程,a[i][j]表示用j分钟学习第i门课程所能获得的价值,背包容量为一共有m时间,求最大价值. 思路: P06: 分组的背包问题 问题 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组,每组中的物品互相冲突,最多选一件.求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 算法 这个问题变成了每组物品有若

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

http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意: 小杰有m天的时间去上n门不同的课. 对于第i门课来说, 如果小杰花j天的时间在该课上, 那么小杰可以获得val[i][j]的价值. 现在给出矩阵val[n][m], 要你求出小杰能获得的最大价值和? 分析: 咋一看, n门课, m天的时间, 要我们求最大价值. 那么明显是从n门课中选出合适的几门, 是的总花费的时间<=m的前提下, 价值最大化. 但是发现每门课有m种不同的价值获取方式. 所以我们