P1270 “访问”美术馆(树形dp)

题目连接:https://www.luogu.com.cn/problem/P1270

 

思路:

一开始想错了,以为是树上背包,后来发现每个非叶子结点会存在时间分配不同的问题,所以只要枚举每个非叶子结点对两棵子树分配的时间多少就好了。

 

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
struct Node
{
    int w1,w2;
}cur[N<<2];
int tim,dp[N][605];
void rd(int rt)
{
    scanf("%d",&cur[rt].w1); scanf("%d",&cur[rt].w2);
    cur[rt].w1 <<= 1;
    if(cur[rt].w2 == 0){
        rd(rt<<1);
        rd(rt<<1|1);
    }
}
int dfs(int rt,int t)
{
    t -= cur[rt].w1;
    if(t <= 0) return 0;
    if(~dp[rt][t]) return dp[rt][t];
    if(cur[rt].w2 != 0){
        return min(cur[rt].w2 , t/5);
    }
    int ans = 0;
    for(int i=0;i<=t;i++){
        ans = max(ans,dfs(rt<<1,i) + dfs(rt<<1|1,t-i));
    }
    dp[rt][t] = ans;
    return ans;
}
int main(void)
{
    memset(dp,-1,sizeof dp);
    scanf("%d",&tim); tim--;
    rd(1);
    printf("%d\n",dfs(1,tim));
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值