树上dp and 背包dp进入融合版

· · 算法·理论

基础

在树上做背包类问题的dp

切入真题

栗子

P2015 二叉苹果树

同上

Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
int dp[N][N], n, q;
struct node{
    int v, w;
};
vector<node> g[N];
void dfs(int st, int fa){
    int cnt = 0, W[2] = {}, ch[2] = {};
    for(int i = 0; i < g[st].size(); i++){
        int v = g[st][i].v, w = g[st][i].w;
        if(v == fa) continue;
        dfs(v, st);
        ch[cnt] = v;
        W[cnt] = w;
        cnt ++;
    }
    if(cnt == 2){
        for(int j = 1; j <= q; j++){
            dp[st][j] = max(dp[ch[1]][j - 1] + W[1], dp[ch[0]][j - 1] + W[0]);
            for(int k = 0; k <= j - 2; k++){    
                dp[st][j] = max(dp[st][j], dp[ch[0]][k] + dp[ch[1]][j - 2 - k] + W[1] + W[0]);
            }
        }
    }
}
void work(){
    //please write your code here
    cin >> n >> q;
    for(int i = 1; i < n; i++){
        int x, y, w;
        cin >> x >> y >> w;
        g[x].push_back({y, w});
        g[y].push_back({x, w});
    }
    dfs(1, 0);
    cout << dp[1][q] << "\n";
}
signed main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}

P1757 通天之分组背包

简单的分组背包

#include<bits/stdc++.h>
#define ll long long
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 1005;
const int M = 100005;
const int INF = 1e18;
const int mod = 998244353;
struct node{
    int a, b;
};
int n, m;
int dp[1005];
vector<node> v[1005];
void work(){
    //please write your code here
    memset(dp, -0x3f, sizeof dp);
    dp[0] = 0;
    cin >> m >> n;
    int maxn = 0;
    for(int i = 1; i <= n; i++){
        int a , b , c;
        cin >> a >> b >> c;
        v[c].push_back({a , b});
        maxn = max(maxn, c);
    }
    int ans = 0;
    for(int i = 1; i <= maxn; i++){
        for(int j = m; j >= 0; j--){
            for(int k = 0; k < v[i].size(); k++){
                if(j >= v[i][k].a){
                    dp[j] = max(dp[j], dp[j - v[i][k].a] + v[i][k].b);
                }
            }
            ans = max(ans, dp[j]);
        }
    }
    cout << ans;
}
problem T1(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}

P2014 [CTSC1997] 选课

这题和上题很像,用上分组背包的知识

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
int n, m, dp[N][N], a[N], siz[N], len[N], top;
vector<int> g[N]; 
void dfs(int u){
    len[++top] = u;
    siz[u] = 1;
    for(int v : g[u]){
        dfs(v);
        siz[u] += siz[v];
    }
    return;
}
void work(){
    //please write your code here
    cin >> n >> m;
    for(int i = 1, x; i <= n; i++){
        cin >> x >> a[i];
        g[x].push_back(i);
    }
    dfs(0);
    for(int i = n + 1; i >= 1; i--){
        for(int j = 1; j <= m + 1; j++){
            dp[i][j] = max(dp[i + siz[len[i]]][j], dp[i + 1][j - 1] + a[len[i]]);
        }
    }
    cout << dp[1][m + 1] << "\n"; 
}
signed main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}
The End.
\ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\
\textcolor{#FE4C61}{\mathrm{r}}\textcolor{#F39C11}{\mathrm{a}}\textcolor{FFC116}{\mathrm{i}}\textcolor{#52C41A}{\mathrm{n}}\textcolor{#3498DB}{\mathrm{b}}\textcolor{#9D3DCF}{\mathrm{o}}\textcolor{#0E1D69}{\mathrm{w}}\ \,\textcolor{#52C41A}{\mathrm{e}}\textcolor{#E74C3C}{\mathrm{g}}\textcolor{#9D3DCF}{\mathrm{g}}
\textcolor{#FE4C61}{\mathrm{r}}\textcolor{#F39C11}{\mathrm{a}}\textcolor{FFC116}{\mathrm{i}}\textcolor{#52C41A}{\mathrm{n}}\textcolor{#3498DB}{\mathrm{b}}\textcolor{#9D3DCF}{\mathrm{o}}\textcolor{#0E1D69}{\mathrm{w}}\ \,\textcolor{#52C41A}{\mathrm{e}}\textcolor{#E74C3C}{\mathrm{g}}\textcolor{#9D3DCF}{\mathrm{g}}