zoukankan      html  css  js  c++  java
  • hdu 2602 Bone Collector 01背包

    Bone Collector

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 17590    Accepted Submission(s): 6954


    Problem Description
    Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
    The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
     
    Input
    The first line contain a integer T , the number of cases.
    Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
     
    Output
    One integer per line representing the maximum of the total value (this number will be less than 231).
     
    Sample Input
    1 5 10 1 2 3 4 5 5 4 3 2 1
     
    Sample Output
    14
     
    最近比较忙,在看01背包,也不知道为什么,好久不做题了,似乎什么也不会,01背包的一维的优化怎么也看不懂,今天看了一个博客,画了一个表,终于明白了。菜鸟还要努力啊!搞ACM这么长时间连个动态规划还处于入门阶段……
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 int main(void){
     8 #ifndef ONLINE_JUDGE
     9   freopen("2602.in", "r", stdin);
    10 #endif
    11   const int N = 1000+10;
    12   int t, n, V, w[N], c[N], f[N];
    13   scanf("%d", &t);
    14   while (t--){
    15     scanf("%d%d", &n, &V);
    16     for (int i = 1; i <= n; ++i) scanf("%d", w+i);
    17     for (int i = 1; i <= n; ++i) scanf("%d", c+i);
    18     memset(f, 0, sizeof(f));
    19     for (int i = 1; i <= n; ++i){
    20       for (int v = V; v >= c[i]; --v){
    21         f[v] = max(f[v], f[v-c[i]] + w[i]);
    22       }
    23     }
    24     printf("%d\n", f[V]);
    25   }
    26   return 0;
    27 }

    引用一个博客,用于理解01背包,尤其是那个图表,自己画一遍,就基本理解为什么可以优化成一维数组了!

    http://www.wutianqi.com/?p=539

  • 相关阅读:
    Linux启动网卡时出现RTNETLINK answers: File exists错误解决方法
    在Linux系统中应用su和sudo
    Python模块常用的几种安装方式
    安装php并使用nginx连接PHP
    查看Chrome浏览器扩展程序源码的两种方法
    angularJs中缓存数据,免去重复发起请求的几种写法
    underscore.js 分析6 map函数
    AngularJS中Directive指令系列
    $watch和$observe的使用
    $watch, $watchCollection, $watchGroup的使用
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2986189.html
Copyright © 2011-2022 走看看