当前位置:网站首页>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;
}
边栏推荐
- 【推荐系统学习】推荐系统架构
- Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
- [qt learning notes]qt inter thread data communication and data sharing
- Leetcode - 226. Retourner l'arbre binaire (bfs)
- Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack
- Can Luo Yonghao succeed in entering the AR field this time?
- Microservice architecture practice: user login and account switching design, order query design of the mall
- NFT 交易市场社区所有化势不可挡
- Cache breakdown! Don't even know how to write code???
- What does the equals method compare? Who told you
猜你喜欢

Platform management background and merchant menu resource management: access control design of platform management background

【代码随想录-动态规划】T583、两个字符串的删除操作

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

14 MySQL tutorial insert insert data

Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!

Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice

【推荐系统学习】推荐系统架构

How sparksql returns a specific day of the week by date -dayofweek function

20:第三章:开发通行证服务:3:在程序中,打通redis服务器;(仅仅是打通redis服务器,不涉及具体的业务开发)

Leetcode HOT100 (22--- bracket generation)
随机推荐
Wechat app mall, review products, upload commodity pictures, and score Commodity Services
Fire evacuation and self rescue... This safety production and fire training is full!
Daily record 2
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
Problems encountered this week
QPushButton 样式使用示例(以及按钮setmenu添加下拉菜单的方法)
#25class的类继承
Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
[recommendation system learning] technology stack of recommendation system
去中心化NFT交易协议将击败OpenSea
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)
Cache breakdown! Don't even know how to write code???
MySQL index
Platform management background and merchant menu resource management: Design of platform management background data service
Use middleware to record slow laravel requests
离婚协议中的几个重点
玩转Linux,轻松安装配置MySQL
SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
物联网协议的王者:MQTT
What is the difference between digital collections and NFT