当前位置:网站首页>有依赖的背包问题
有依赖的背包问题
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;
}
边栏推荐
- Screenshot of the answers to C language exercises
- Convert the decimal positive integer m into the number in the forward K (2 < =k < =9) system and output it in bits
- Use FST JSON to automatically generate faster JSON serialization methods
- The texstudio official website cannot be opened
- Calculate a=1, a2=1/1=a1
- Interpretation of cloud native microservice technology trend
- Leetcode topic [array] -268- missing numbers
- When I was in the library, I thought of the yuan sharing mode
- 直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
- 玩轉Linux,輕松安裝配置MySQL
猜你喜欢

宝藏又小众的CTA动画素材素材网站分享

Notes on flowus

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)

Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
![[recommendation system learning] technology stack of recommendation system](/img/ff/afc6f4b0997cfcb9e01ffbebf2a872.png)
[recommendation system learning] technology stack of recommendation system
![[suggested collection] 11 online communities suitable for programmers](/img/6b/d5c68e93384fd314d0cb27d9df1cb9.jpg)
[suggested collection] 11 online communities suitable for programmers

Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)

Byte interview: two array interview questions, please accept

Teach you to learn dapr - 5 Status management

C language --- basic function realization of push box 01
随机推荐
Leetcode HOT100 (22--- bracket generation)
宝藏又小众的CTA动画素材素材网站分享
Community ownership of NFT trading market is unstoppable
玩转Linux,轻松安装配置MySQL
Teach you to learn dapr - 6 Publish subscription
The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
Programmer's essential toolkit, please collect!
Web3 decentralized storage ecological landscape
关于FlowUs这一款国民好笔记
Redis' 43 serial cannons, try how many you can carry
Platform management background and merchant menu resource management: access control design of platform management background
A simple membership card management system based on Scala
Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
Ndroid development from introduction to mastery Chapter 2: view and ViewGroup
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
The texstudio official website cannot be opened
Deployment and operation of mongodb partitioned cluster
Record the use process of fenics
Secrets of gear contract
Viewing the task arrangement ability of monorepo tool from turborepo