算法【有依赖的背包】

有依赖的背包是指多个物品变成一个复合物品(互斥),每件复合物品不要和怎么要多种可能性展开。时间复杂度O(物品个数 * 背包容量),额外空间复杂度O(背包容量)。

下面通过题目加深理解。

题目一

测试链接:[NOIP2006 提高组] 金明的预算方案 - 洛谷

分析:对于这道题,可以参考01背包是对每个物品进行可能性的展开,有依赖的背包是对主件进行可能性的展开,所以可能性就比01背包的展开多。对于一个没有附件的主件可能性的展开,就是01背包的展开,即选或不选主件。对于有一个附件的主件可能性的展开,就有三种,选主件、不选主件、主件和附件一起选。对于有两个附件的主件可能性的展开,就有五种,选主件、不选主件、主件和第一个附件一起选、主件和第二个附件一起选、主件和两个附件一起选。对于输入,代码中采用了几个数组结构存储信息,cost数组存储花费代价,value数组存储收益,king数组存储是否是主件,fans数组存储主件有多少个附件,follows数组存储每个主件拥有的附件。下面代码采用计划搜索,并没有去做空间压缩,代码如下。

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
int n, m;
int cost[61];
int value[61];
bool king[61];
int fans[61] = {0};
vector<vector<int>> follows;
int dp[61][32001];
int f(int index, int money){
    if(index == m+1){
        return 0;
    }
    if(dp[index][money] != -1){
        return dp[index][money];
    }
    if(!king[index]){
        return f(index+1, money);
    }
    int ans = f(index+1, money);
    if(money - cost[index] >= 0){
        ans = ans > f(index+1, money-cost[index]) + value[index] ?
        ans : f(index+1, money-cost[index]) + value[index];
    }
    if(fans[index] >= 1 && money - cost[index] - cost[follows[index][0]] >= 0){
        ans = ans > f(index+1, money-cost[index]-cost[follows[index][0]]) + value[index] + value[follows[index][0]] ?
        ans : f(index+1, money-cost[index]-cost[follows[index][0]]) + value[index] + value[follows[index][0]];
    }
    if(fans[index] == 2){
        if(money - cost[index] - cost[follows[index][1]] >= 0){
            ans = ans > f(index+1, money-cost[index]-cost[follows[index][1]]) + value[index] + value[follows[index][1]] ?
            ans : f(index+1, money-cost[index]-cost[follows[index][1]]) + value[index] + value[follows[index][1]];
        }
        if(money - cost[index] - cost[follows[index][0]] - cost[follows[index][1]] >= 0){
            ans = ans > f(index+1, money-cost[index]-cost[follows[index][0]]-cost[follows[index][1]]) + value[index] + value[follows[index][0]] + value[follows[index][1]] ?
            ans : f(index+1, money-cost[index]-cost[follows[index][0]]-cost[follows[index][1]]) + value[index] + value[follows[index][0]] + value[follows[index][1]];
        }
    }
    dp[index][money] = ans;
    return ans;
}
int main(void){
    int v, p, q;
    scanf("%d%d", &n, &m);
    vector<int> temp;
    for(int i = 0;i <= m;++i){
        follows.push_back(temp);
    }
    for(int i = 1;i <= m;++i){
        scanf("%d%d%d", &v, &p, &q);
        cost[i] = v;
        value[i] = v * p;
        if(q != 0){
            king[i] = false;
            fans[q]++;
            follows[q].push_back(i);
        }else{
            king[i] = true;
        }
    }
    for(int i = 1;i < 61;++i){
        for(int j = 1;j < 32001;++j){
            dp[i][j] = -1;
        }
    }
    printf("%d", f(1, n));
    return 0;
}
相关推荐
fufu03113 分钟前
Linux环境下的C语言编程(三十七)
算法
风筝在晴天搁浅12 分钟前
代码随想录 300.最长递增子序列
算法·动态规划
小O的算法实验室12 分钟前
2026年EAAI SCI1区TOP,基于进化算法的多目标施工现场布局与安全规划模型,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
.小小陈.14 分钟前
C++初阶5:string类使用攻略
开发语言·c++·学习·算法
代码游侠18 分钟前
数据结构——树
数据结构·算法
天骄t20 分钟前
树与哈希:数据结构核心解析
数据结构·算法
小年糕是糕手23 分钟前
【C++】类和对象(六) -- 友元、内部类、匿名对象、对象拷贝时的编译器优化
开发语言·c++·算法·pdf·github·排序算法
ShineLeong24 分钟前
C的第一次
数据结构·算法
是宇写的啊1 小时前
算法-前缀和
算法
Brduino脑机接口技术答疑1 小时前
脑机接口数据处理连载(六) 脑机接口频域特征提取实战:傅里叶变换与功率谱分析
人工智能·python·算法·机器学习·数据分析·脑机接口