zoukankan      html  css  js  c++  java
  • 杭电2602--Bone Collector(01背包)

     
    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
     

    Author
    Teddy
     

    Source
     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1203 2159 2955 1171 2191 
     
    RE: 01背包基础题:
     
    //感觉这个比较好理解:   46ms  
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define max(a, b) a>b?a:b
    using namespace std;
    int dp[1010][1010], val[1010], vol[1010];
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            int n, v;
            scanf("%d %d", &n, &v);
            for(int i = 1; i <= n; i++)
                scanf("%d", &val[i]);
            for(int i = 1; i <= n; i++)
                scanf("%d", &vol[i]);
            for(int i = 1; i <= n; i++)
            {
                for(int j = 0; j <= v; j++)  //容量; 
                {
                    if(j < vol[i])
                        dp[i][j] = dp[i-1][j];
                    else
                        dp[i][j] = max(dp[i-1][j], dp[i-1][j-vol[i]] + val[i]);    
                } 
            }
            printf("%d
    ", dp[n][v]);
        }
        return 0;    
    } 
    //比二维数组稍微快点; 31ms 
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define max(a, b) a>b?a:b 
    using namespace std;
    int dp[1010], val[1010], vol[1010];
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            int n, v;
            scanf("%d %d", &n, &v);
            for(int i = 1; i <= n; i++)
                scanf("%d", &val[i]);
            for(int j = 1; j <= n; j++)
                scanf("%d", &vol[j]);
            memset(dp, 0, sizeof(dp)); 
            for(int k = 1; k <= n; k++)
            {
                for(int Q = v; Q >= vol[k]; Q--)
                {
                    dp[Q] = max(dp[Q], dp[Q - vol[k]] + val[k]); 
                } 
                
            }
            printf("%d
    ", dp[v]);
        }
        return 0;    
    } 
  • 相关阅读:
    mysql权限
    Win7_64位使用Mysql Odbc
    二叉树的遍历
    Notepad++的使用
    mysql与mysqld
    Mysql 聚集函数和分组
    Linux 目录
    Linux 倒引号、单引号、双引号
    openkm安装过程
    rhel 7 设置默认运行级别为图形
  • 原文地址:https://www.cnblogs.com/soTired/p/4763252.html
Copyright © 2011-2022 走看看