当前位置:网站首页>有依赖的背包问题
有依赖的背包问题
2022-06-26 17:05:00 【Zqchang】
背包和树形dp的结合版
这个题目是一个树的形式,所以用递归的思路来考虑
因为如果要选择子节点,必须要选择根节点,所以用递归比较方便
集合划分的时候,先将集合按照子树有多少划分一下,然后再按照体积划分一下
这里不同于金明的预算方案,金明的预算方案是按照方案来进行划分,因为它的方案数比较少,而这个题目中方案数比较多,因为节点个数可能很多,所以用体积来划分,就比如,左边第一个子树,用的体积是0的情况下,它的最大价值是多少,体积是1。。。依次往后推,一共m+1种选法(体积最大是m)

这样就能看成是一个分组背包,把每一个子树看成是一个物品组。每个物品组有m+1个物品,第0个物品表示体积是0,价值是f[0],第五个表示是,体积是5,价值是f[5],第m类表示体积是m,价值是f[m],其实就是把每一个子树看成是一个物品组,每一个物品组种只能选一个
dp是怎么优化的呢?就是用某个数表示一类
本质上是一个树形dp的框架,然后在树形dp的框架之下,我们每一次就只需要考虑,当前的根节点和它的子节点之间的关系,就只需要考虑两层之间的关系,在考虑两层之间关系的时候,我们可以发现,可以把每一个子树,看成是一个物品组,这就是一个分组背包的问题,就可以用分组背包的方式解决
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m;
int v[N], w[N];
int h[N], e[N], ne[N], idx;
int f[N][N];//存的i是点的编号,j是用了多少体积
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void dfs(int u)
{
for (int i = h[u]; ~i; i = ne[i]) // 循环物品组
{
int son = e[i];
dfs(e[i]);
// 分组背包
for (int j = m - v[u]; j >= 0; j -- ) // 循环体积
for (int k = 0; k <= j; k ++ ) // 循环决策
f[u][j] = max(f[u][j], f[u][j - k] + f[son][k]);
}
//自我感觉可以这么考虑,就是分配给这个子树了j体积,如果j大于v[u],说明在子树中选了东西,所以u一定要加进去
// 将物品u加进去
for (int i = m; i >= v[u]; i -- ) f[u][i] = f[u][i - v[u]] + w[u];
for (int i = 0; i < v[u]; i ++ ) f[u][i] = 0;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
int root;
for (int i = 1; i <= n; i ++ )
{
int p;
cin >> v[i] >> w[i] >> p;
if (p == -1) root = i;
else add(p, i);
}
dfs(root);
cout << f[root][m] << endl;
return 0;
}
边栏推荐
- Prometeus 2.34.0 新特性
- Swap two numbers
- Detailed contract quantification system development scheme and technical description of quantitative contract system development
- Multiply the values of the upper triangular elements of the array by M
- Implementation of MySQL master-slave architecture
- Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
- Byte interview: two array interview questions, please accept
- Romance of the Three Kingdoms: responsibility chain model
- Teach you to learn dapr - 5 Status management
- Discover K8E: minimalist kubernetes distribution
猜你喜欢

20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)

Distributed Architecture Overview

Getting started with mongodb

Set up your own website (16)

【万字总结】以终为始,详细分析高考志愿该怎么填

The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious

牛客网:设计LRU缓存结构 设计LFU缓存结构

Cache breakdown! Don't even know how to write code???

Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)

10 cloud security best practices that enterprises need to know
随机推荐
关于FlowUs这一款国民好笔记
Multiply the values of the upper triangular elements of the array by M
类型多样的石膏PBR多通道贴图素材,速来收藏!
Sandboxed container: container or virtual machine
MySQL index
When I was in the library, I thought of the yuan sharing mode
数字藏品与NFT到底有何区别
【动态规划】剑指 Offer II 091. 粉刷房子
并发之线程安全
The texstudio official website cannot be opened
Discussion: the next generation of stable coins
Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
Count the number of each vowel letter in the string
Concurrent thread safety
Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
Byte interview: two array interview questions, please accept
Classical synchronization problem
并发之Synchronized说明
Fire evacuation and self rescue... This safety production and fire training is full!
Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack