01背包问题之HDU——2602 Bone Collector

本文详细解析了01背包问题的定义及解决方法,通过一个具体的骨收集者问题实例介绍了如何使用动态规划来求解此类问题,同时给出了两种实现方式及其优化方案。

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">01背包问题:</span>

01背包问题是说有一个重量为w的包,有n个物体,第i个物体的重量是w[I],价值是V[I]每个物体最多只能拿一次每个物体只有一个怎样尽可能地装满包,使得价值最大,并且总的体重不超过w;

 

分析:之所以称为01背包是因为物体可以拿也可以不拿。那么分析一下状态,我们需要知道取了第几个物体,以及此时的背包大小最多可装多少价值的物体。

dp[i][j]:i表示取到第i个物体;j表示背包容量为j;dp[i][j]表示最大价值。

状态转移:取到第i个物体背包容量可以装下时,我们可以要,也可以不要,什么时候要这个物体?

————>当要第i个物体时:dp[i][j]=dp[i-1][j-w[i]]+v[i];(为什么是j-w[i]?模拟一下背包放物体的过程我们就知道,当要放第i个物体时,背包容量就为j-w[i],剩余背包尽可能多地装前面i-1个物体。)

————>当第i个物体不装时,那么dp[i][j]=dp[i-1][j];

取两者中的最大值。得到的便是最优解。

例题:

Bone Collector

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


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
 

Author
Teddy
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1005;
int dp[maxn][maxn];
int va[maxn];
int w[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        int n,v;
        scanf("%d%d",&n,&v);
        for(int i=1;i<=n;i++)
            scanf("%d",&va[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=v;j++)
            {
                if(j<w[i])
                    dp[i][j]=dp[i-1][j];//如果背包的容量比第i个物体小,放不下这个物体,相当于不放。
                else
                    dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+va[i]);//状态转移。
            }
        }
        printf("%d\n",dp[n][v]);
    }
}

优化如下:
由于当更新到第i个物体是否可放时,我们只需要用到第i-1个物体的状态,因此只需要用一个一维数组记录背包为j时的最大价值,但是要注意要从最大容量更新,因为我们会用到前面小容量的值,从小容量更新会破坏原始数据。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1005;
int dp[maxn];//容量为j的背包能放的最大价值
int va[maxn];
int w[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        int n,v;
        scanf("%d%d",&n,&v);
        for(int i=1;i<=n;i++)
            scanf("%d",&va[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=v;j>=w[i];j--)//j从最大更新,只需要更新到w[i],因为当背包装不下i时无需更新,已经是最大了。
            {
                dp[j]=max(dp[j],dp[j-w[i]]+va[i]);
            }
        }
        printf("%d\n",dp[v]);
    }
}


考虑可再生能源出力不确定性的商业园区用户需求响应策略(Matlab代码实现)内容概要:本文围绕“考虑可再生能源出力不确定性的商业园区用户需求响应策略”展开,结合Matlab代码实现,研究在可再生能源(如风电、光伏)出力具有不确定性的背景下,商业园区如何制定有效的需求响应策略以优化能源调度和提升系统经济性。文中可能涉及不确定性建模(如场景生成与缩减)、优化模型构建(如随机规划、鲁棒优化)以及需求响应机制设计(如价格型、激励型),并通过Matlab仿真验证所提策略的有效性。此外,文档还列举了大量相关的电力系统、综合能源系统优化调度案例与代码资源,涵盖微电网调度、储能配置、负荷预测等多个方向,形成一个完整的科研支持体系。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事能源系统规划与运行的工程技术人员。; 使用场景及目标:①学习如何建模可再生能源的不确定性并应用于需求响应优化;②掌握使用Matlab进行商业园区能源系统仿真与优化调度的方法;③复现论文结果或开展相关课题研究,提升科研效率与创新能力。; 阅读建议:建议结合文中提供的Matlab代码实例,逐步理解模型构建与求解过程,重点关注不确定性处理方法与需求响应机制的设计逻辑,同时可参考文档中列出的其他资源进行扩展学习与交叉验证。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值