zoukankan      html  css  js  c++  java
  • Bone Collector-HDU

    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
    Author
    Teddy
    Source
    思路:
     二维代码:
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    using namespace std;
    long long max(long long num1,long long num2)//比较函数
    {
    return num1>num2?num1:num2;
    }
    long long dp[1005][1005];//定义背包数组
    int main()
    {
        int t;//数据组数
        int m_bone,m_volume;
        int bone[1005], volume[1005];
        cin >> t;
        while (t--)
        {
            int i, j, k;
            memset(dp, 0, sizeof(dp));//初始化
            memset(bone,0,sizeof(bone));
            memset(volume, 0, sizeof(volume));
            cin >> m_bone >> m_volume;//输入骨头总数和容量
            for (i = 1; i <= m_bone; i++)
                cin >> bone[i];//输入价值
            for (j = 1; j <= m_bone; j++)
                cin >> volume[j];//输入容量
            for (i = 1; i <= m_bone; i++)
            {
                for (j = 0; j <= m_volume; j++)
                {
                    if (j < volume[i])//如果装不下
                    {
                        dp[i][j] = dp[i - 1][j];//将上一个值赋给当前的价值
                        continue;
                    }
                    else
                    {
                        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - volume[i]] + bone[i]);//动态转移方程
                    }
                }
            }
            cout << dp[m_bone][m_volume] << endl;//输出价值最大
        }
        return 0;
    } 
    优化成:一维代码:
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    using namespace std;
    long long max(long long num1,long long num2)//比较函数
    {
    return num1>num2?num1:num2;
    }
    long long dp[1005];//定义背包数组
    int main()
    {
    int N;
    int i,j,k;
    int bone[1005];//定义价值
    int volume[1005];//定义容量
    int t;
    scanf("%d",&t);
    while(t--)
    {
    memset(dp,0,sizeof(dp));//初始化
    int count,weight;
    scanf("%d%d",&count,&weight);//输入组数和总重量
    for(j=1;j<=count;j++)
    scanf("%d",&bone[j]);
    for(j=1;j<=count;j++)
    scanf("%d",&volume[j]);
    for(i=1;i<=count;i++)
    {
        for(k=weight;k>=0;k--)
    {
            if(k-volume[i]<0)//装不下
            {
                break;
            }
            else
                dp[k]=max(dp[k-volume[i]]+bone[i],dp[k]);
            cout << dp[k] << endl;
        }
    }
    printf("%lld
    ",dp[weight]);
    }
    return 0;
    }
    以大多数人努力程度之低,根本轮不到去拼天赋~
  • 相关阅读:
    不同路径II --动态规划
    观察者模式-设计模式
    策略模式-设计模式
    不同路径--动态规划
    最长回文子串--动态规划
    模板方法-设计模式
    爬楼梯-动态规划
    最大子序和 --动态规划
    R.Swift优雅加载资源文件
    最大公约数和最小公倍数
  • 原文地址:https://www.cnblogs.com/gcter/p/7459420.html
Copyright © 2011-2022 走看看