题目链接

CF815C Karen and Supermarket

题解

只要在最大化数量的前提下,最小化花费就好了

这个数量枚举ok,

dp[i][j][1/0]表示节点i的子树中买了j件商品 i 优惠了 / 没优惠

复杂度是n^2的

因为每次是新儿子节点的siz * 之前儿子几点的siz,

就相当于树上的节点两两匹配,这个匹配只会在lca处计算一次

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') { if(c == '-')f = -1; c = getchar(); }
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
const int maxn = 5007;
int n; LL b;int c[maxn], d[maxn];
struct node {
int v,nxt;
} edge[maxn];
int head[maxn],num = 0 ;
inline void add_edge(int u,int v) {
edge[++ num].v = v; edge[num].nxt = head[u];head[u] = num;
}
LL dp[maxn][maxn][2];
int siz[maxn];
void dfs(int x) {
siz[x] = 1;
dp[x][0][0] = 0;
dp[x][1][0] = c[x] ;
dp[x][1][1] = c[x] - d[x];
//for(int i = head[x];i;i = edge[i].nxt) dfs(edge[i].v), siz[x] += siz[edge[i].v];
for(int i = head[x];i;i = edge[i].nxt) {
int v = edge[i].v;
dfs(v);
for(int j = siz[x];j >= 0;-- j) {
for(int k = 0;k <= siz[v];++ k) {
dp[x][j + k][0] = std::min(dp[x][j + k][0],dp[x][j][0] + dp[v][k][0]);
dp[x][j + k][1] = std::min(dp[x][j + k][1],dp[x][j][1] + std::min(dp[v][k][1],dp[v][k][0]));
}
} siz[x] += siz[v];
}
}
int main() {
memset(dp,0x3f,sizeof dp);
n = read(), b = read();
c[1] = read(); d[1] = read();
for(int pre, i = 2;i <= n;++ i) {
c[i] = read(),d[i] = read(); pre = read();
add_edge(pre,i);
}
dfs(1);
int ans = 0;
for(int i = 1;i <= n;++ i)
if(std::min(dp[1][i][0],dp[1][i][1]) <= b)ans = i;
printf("%d\n",ans);
return 0;
}

CF815C Karen and Supermarket的更多相关文章

  1. CF815C Karen and Supermarket [树形DP]

    题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...

  2. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

  3. E. Karen and Supermarket

    E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

  4. Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP

    C. Karen and Supermarket     On the way home, Karen decided to stop by the supermarket to buy some g ...

  5. 「CF815C」Karen and Supermarket

    传送门 Luogu 解题思路 树形背包. 设 \(f[i][j][0/1]\) 表示在以 \(i\) 为根的子树中选 \(j\) 件商品的最少花费. 边界条件: \(f[i][j][0] = \min ...

  6. codeforces 815C Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  7. codeforces round #419 E. Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  8. Codeforces 815 C Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  9. 【Codeforces 815C】Karen and Supermarket

    Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...

随机推荐

  1. Openstack 网络服务 Neutron计算节点部署(十)

    Neutron计算节点部署 安装组件,安装的服务器是192.168.137.12 1.安装软件包 yum install -y openstack-neutron-linuxbridge ebtabl ...

  2. Spark记录-Scala模式匹配

    Scala模式匹配 模式匹配是Scala函数值和闭包后第二大应用功能.Scala为模式匹配提供了极大的支持,处理消息. 模式匹配包括一系列备选项,每个替代项以关键字大小写为单位.每个替代方案包括一个模 ...

  3. 深度学习 vs. 概率图模型 vs. 逻辑学

    深度学习 vs. 概率图模型 vs. 逻辑学 摘要:本文回顾过去50年人工智能(AI)领域形成的三大范式:逻辑学.概率方法和深度学习.文章按时间顺序展开,先回顾逻辑学和概率图方法,然后就人工智能和机器 ...

  4. [转]Centos 安装Sublime text 3

    本文简单介绍在CentOS上安装Sublime text 3, 转自:Centos 安装Sublime text 3 Step 1 :建立软件安装目录 # mkdir /opt # cd /opt S ...

  5. mysql学习------错误日志和通用查询日志

    一.启动错误日志 1.在不同情况下,错误日志会记录在不同的位置.如果没有在配置文件中指定文件名,则文件名默认为hostname.err 2.在mysql5.6的rpm发布方式中,错误的日志默认的放置在 ...

  6. 用Python连接SQLServer抓取分析数据、监控 (pymssql)

    Python 环境:python3 服务器环境: centos6.5 数据库: Mysql 大概流程:在装有Python服务器,利用pymssql库连接MSSQL生产数据库取出数据然后写进mysql数 ...

  7. ipsec-tools安装教程

    ipsec-tools最新版本为0.8.2,此处以0.7.3版本为例说明安装和使用过程.可参考ipsec-howto. 安装步骤 ipsec-tools依赖于linux2.6版本内核,在安装ipsec ...

  8. Android BLE设备蓝牙通信框架BluetoothKit

    BluetoothKit是一款功能强大的Android蓝牙通信框架,支持低功耗蓝牙设备的连接通信.蓝牙广播扫描及Beacon解析. 关于该项目的详细文档请关注:https://github.com/d ...

  9. 转:vue-router 2.0 常用基础知识点之router.push()

    转载地址:http://www.jianshu.com/p/ee7ff3d1d93d router.push(location) 除了使用 <router-link> 创建 a 标签来定义 ...

  10. .NetCore生产环境下构建Consul(单个DC数据中心)的服务器健康检查

    下载最新的consul程序 consul 启动方式有两种 server 和client 默认是client 如果你不需要记录数据可以用 consul agent -dev 来启动 consul age ...