当前位置:网站首页>Knapsack problem with dependency
Knapsack problem with dependency
2022-06-26 17:26:00 【Zqchang】
Backpack and tree dp Combined version of
This topic is in the form of a tree , So we use the idea of recursion to consider
Because if you want to select a child node , You must select the root node , So recursion is more convenient 
When the set is divided , First, divide the set according to the number of subtrees , Then divide it by volume 
This is different from Jinming's budget plan , Jinming's budget plan is divided according to the plan , Because it has a small number of schemes , There are many schemes in this topic , Because there may be many nodes , So we use volume to divide , Like , The first subtree on the left , The volume used is 0 Under the circumstances , What is its greatest value , The volume is 1... Push back in turn , altogether m+1 Seed selection ( The largest volume is m)

In this way, it can be regarded as a group backpack , Think of each subtree as a group of items . Each item group has m+1 Items , The first 0 Items represent a volume of 0, The value is f[0], The fifth expression is , The volume is 5, The value is f[5], The first m Class represents that the volume is m, The value is f[m], In fact, each subtree is regarded as an item group , Only one item can be selected for each item group
dp How to optimize it ? Is to use a certain number to represent a class
It is essentially a tree dp Framework , And then in the tree dp Under the framework of , We only need to consider each time , The relationship between the current root node and its child nodes , Just consider the relationship between the two layers , When considering the relationship between the two layers , We can find out , You can put every sub tree , Think of it as a group of items , This is the problem of grouping backpacks , It can be solved by grouping backpacks
#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];// Deposited i Is the number of points ,j Is the volume used
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]) // Recycle item group
{
int son = e[i];
dfs(e[i]);
// Pack in groups
for (int j = m - v[u]; j >= 0; j -- ) // Circulation volume
for (int k = 0; k <= j; k ++ ) // Circular decision
f[u][j] = max(f[u][j], f[u][j - k] + f[son][k]);
}
// I feel I can think about it this way , Is assigned to this subtree j Volume , If j Greater than v[u], It means that something is selected in the subtree , therefore u Be sure to add
// Put the object u add
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;
}
边栏推荐
- She said she was tired! So tired! I want to change my career
- Environment setup mongodb
- 背包问题求方案数
- LeetCode——226. Flip binary tree (BFS)
- Fire evacuation and self rescue... This safety production and fire training is full!
- 牛客网:设计LRU缓存结构 设计LFU缓存结构
- 类型多样的石膏PBR多通道贴图素材,速来收藏!
- 分布式架构概述
- What does the equals method compare? Who told you
- Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
猜你喜欢

Teach you to learn dapr - 3 Run the first with dapr Net program

What is the difference between digital collections and NFT

MySQL add column failed because there was data before, not null by default

#25class的类继承

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)

防火 疏散 自救…这场安全生产暨消防培训干货满满!

Jouer avec Linux et installer et configurer MySQL facilement

Niuke network: Design LRU cache structure design LFU cache structure

Platform management background and merchant menu resource management: merchant registration management design

In those years, interview the abused red and black trees
随机推荐
Classical synchronization problem
【推荐系统学习】推荐系统的技术栈
Romance of the Three Kingdoms: responsibility chain model
LeetCode——226. 翻转二叉树(BFS)
Microservice architecture practice: business management background and SSO design, SSO client design
Platform management background and merchant menu resource management: merchant registration management design
Decentralized NFT transaction protocol will defeat opensea
一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
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)
在国金证券开户怎么样?开户安全吗?
Redis and database data consistency
关于FlowUs这一款国民好笔记
SQL injection for Web Security (3)
Use middleware to record slow laravel requests
She said she was tired! So tired! I want to change my career
How sparksql returns a specific day of the week by date -dayofweek function
【万字总结】以终为始,详细分析高考志愿该怎么填
Leetcode daily [2022 - 02 - 16]
Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
Distributed Architecture Overview