• hdu 1712(分组背包)


    ACboy needs your help

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


    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
     
    题意:ACBOY有n种功课要做,他总共有m天做这些功课,如果利用j天在功课i上,他能够获得的最大价值是a[i][j]问ACBOY如何才能获得最大价值.

    分析:这题ACBOY有n种不同的功课,每种功课最多做一次,典型的分组背包问题.
            dp[i][j] 表示在前i门功课中花费j天能够获得的最大价值,当然写的时候压缩成一维.
    解释一下分组背包的三层循环:
    如果这样写: 那么前两层循环表示在第k组里面做01背包,第三层循环表示在第k组
    里面选择某个物品,这样做的话能够保证在第 k 个组最多选一个。
    for(k=1;k<=K;k++)
        for(int v = V;v>=0;v--)
            for(所有的i属于第k组)
    如果这样写:后两层循环可以看成一个01背包,我们对每组里面的所有物品进行01背包
    ,这样就能够保证在第 k 组里面至少选一件.
    for(k=1;k<=K;k++)
        for(所有的i属于第k组)
            for(int v = V;v>=0;v--)
    至于其他的,碰到再说...
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #define N 105
    using namespace std;
    
    int dp[N];
    int a[N][N];
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF,n+m){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
            }
            memset(dp,0,sizeof(dp));
            for(int k=1;k<=n;k++){
                for(int i=m;i>=0;i--){
                    for(int j=1;j<=i;j++){   ///枚举第 k 组里面所有的天数
                        dp[i] = max(dp[i],dp[i-j]+a[k][j]);
                    }
                }
            }
            printf("%d
    ",dp[m]);
        }
        return 0;
    }
  • 相关阅读:
    P4396 [AHOI2013]作业
    NOIP2018普及T2暨洛谷P5016 龙虎斗
    NOIP2018普及T1暨洛谷P5015 标题统计 题解
    【交题大桥】团队信息存档
    markdown浅谈
    洛谷P1690 贪婪的Copy 题解
    洛谷P4994 终于结束的起点 题解
    洛谷P4995 跳跳!题解
    这么多都变了,洛谷4还会远吗?
    洛谷P1396 营救 题解
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5402625.html
Copyright © 2020-2023  润新知