zoukankan      html  css  js  c++  java
  • hdu 1712 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): 2403    Accepted Submission(s): 1223


    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
     
    分组背包,唉,开始写还是卡了。。卡在第三层循环有木有!j表示花费,所以呢,只能从1循环到v,不能超过去了。也就是说,对于每一组中的物品,要首先看一下这个物品的花费是不是比v小,然后再考虑要不要放进去。还是看的别人的代码才明白的。。次嗷……以后再也不敢了。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 int a[120][120];
     8 int main(void){
     9 #ifndef ONLINE_JUDGE
    10   freopen("1712.in", "r", stdin);
    11 #endif
    12   int n, m, f[200];
    13   while (~scanf("%d%d", &n, &m)){
    14     memset(f, 0, sizeof(f));
    15     if (m+n==0) break;
    16     for (int i = 1; i <= n; ++i)
    17       for (int j = 1; j <= m; ++j)
    18         scanf("%d", &a[i][j]);
    19     for (int i = 1; i <= n ; ++i){
    20       for (int v = m; v >= 0; --v){
    21         for (int j = 1; j <= v; ++j){
    22           f[v] = max(f[v], f[v-j] + a[i][j]);
    23         }
    24       }
    25     }
    26     printf("%d\n", f[m]);
    27   }
    28 
    29   return 0;
    30 }

    理解,关键是理解!不能盲目照着人家的板子抄。。。

  • 相关阅读:
    Node.js:util.inherits 面向对象特性【原型】
    Redbean:入门(二)
    Redbean:入门(一)
    PHP 文件操作函数大全
    Jquer的三种初始化方式
    Php+Redis 实现Redis提供的lua脚本功能
    Redis 五:配置主从复制功能
    Redis 四:存储类型之有序集合
    jQuery选择器总结(转)
    IDEA ECLIPSE Debug 卡住,Run正常
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2988938.html
Copyright © 2011-2022 走看看