Karen and Supermarket

感觉就是很普通的树形dp。

dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费。

dp[ i ][ 1 ][ u ]表示在 i 这棵子树中选择 u 个且 i 用优惠券的最小花费。

注意这个转移总的合起来是O(n ^ 2)的。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, b, d[N], c[N], fa[N];
vector<int> G[N]; int dp[N][][N];
int tmp[N][][N];
int sum[N]; void dfs(int u) {
sum[u] = ;
memset(dp[u], inf, sizeof(dp[u]));
dp[u][][] = ;
dp[u][][] = c[u];
dp[u][][] = c[u] - d[u];
for(auto& v : G[u]) {
dfs(v);
memset(tmp[u], INF, sizeof(tmp[u]));
for(int i = ; i <= sum[u]; i++) {
for(int j = ; j <= sum[v]; j++) {
tmp[u][][i + j] = min(tmp[u][][i + j], dp[u][][i] + dp[v][][j]);
tmp[u][][i + j] = min(tmp[u][][i + j], dp[u][][i] + dp[v][][j]);
tmp[u][][i + j] = min(tmp[u][][i + j], dp[u][][i] + dp[v][][j]);
}
}
sum[u] += sum[v];
memcpy(dp[u], tmp[u], sizeof(dp[u]));
}
} int main() {
scanf("%d%d", &n, &b);
for(int i = ; i <= n; i++) {
scanf("%d%d", &c[i], &d[i]);
if(i > ) {
scanf("%d", &fa[i]);
G[fa[i]].push_back(i);
}
}
dfs();
for(int i = n; i >= ; i--) {
if(dp[][][i] <= b || dp[][][i] <= b) {
printf("%d\n", i);
return ;
}
}
return ;
} /*
*/

Codeforces 815C Karen and Supermarket 树形dp的更多相关文章

  1. 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 ...

  2. CF815C Karen and Supermarket [树形DP]

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

  3. 816E. Karen and Supermarket 树形DP

    LINK 题意:给出n个商品,除第一个商品外,所有商品可以选择使用优惠券,但要求其前驱商品已被购买,问消费k以下能买几个不同的商品 思路:题意很明显就是树形DP.对于一个商品有三种选择,买且使用优惠券 ...

  4. Codeforces 815C. Karen and Supermarket【树形DP】

    LINK 思路 首先发现依赖关系是一个树形的结构 然后因为直接算花多少钱来统计贡献不是很好 因为数组开不下 那就可以算一个子树里面选多少个的最小代价就可以了 注意统计贡献的时候用优惠券的答案只能在1号 ...

  5. 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 ...

  6. CodeForces 816E Karen and Supermarket ——(树形DP)

    题意:有n件商品,每件商品都最多只能被买一次,且有一个原价和一个如果使用优惠券以后可以减少的价格,同时,除了第一件商品以外每件商品都有一个xi属性,表示买这个商品时如果要使用优惠券必须已经使用了xi的 ...

  7. [CF816E] Karen and Supermarket1 [树形dp]

    传送门 - > \(CF816E\) Karen and Supermarket 题意翻译 在回家的路上,凯伦决定到超市停下来买一些杂货. 她需要买很多东西,但因为她是学生,所以她的预算仍然很有 ...

  8. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  9. codeforces 337D Book of Evil (树形dp)

    题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...

随机推荐

  1. DotNetBar滚动条的疑似BUG

    1.重现过程,在winform窗体上拖一个VScrollBarAdv 2.Button里点击跟踪代码 3.Value居然是-5,,而不是0,这是直接赋值,不是手动拖的呀. 4.解决办法,将LargeC ...

  2. QA系统Match-LSTM代码研读

    QA系统Match-LSTM代码研读 背景 在QA模型中,Match-LSTM是较早提出的,使用Prt-Net边界模型.本文是对阅读其实现代码的总结.主要思路是对照着论文和代码,对论文中模型的关键结构 ...

  3. Linux - 包不同安装方式

    rpm 软件包管理器 安装编译包好的二进制包 方式 rpm -ivh lynx # rpm安装 rpm -e lynx # 卸载包 rpm -e lynx --nodeps # 强制卸载 rpm -q ...

  4. 复选框QCheckBox

    复选框一共有三种状态:全选中.半选中和无选中.若一个父选项的子选项全部为选中状态,则该父选项为全选中:若子选项全部为无选中状态,则该父选项为无选中状态:若子选项既有全选中和无选中状态,则该父选项为半选 ...

  5. Spring源码学习资料

    未完待续.. github地址 https://github.com/spring-projects 学习地址 https://github.com/code4craft/tiny-spring 推荐 ...

  6. CNN可解释

    1 http://bindog.github.io/blog/2018/02/10/model-explanation/ http://www.sohu.com/a/216216094_473283 ...

  7. SpringBoot2.x整合Redis实战 4节课

    1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download          2.新手 ...

  8. 论文笔记系列-Efficient Neural Architecture Search via Parameter Sharing

    Summary 本文提出超越神经架构搜索(NAS)的高效神经架构搜索(ENAS),这是一种经济的自动化模型设计方法,通过强制所有子模型共享权重从而提升了NAS的效率,克服了NAS算力成本巨大且耗时的缺 ...

  9. 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

    在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化 ...

  10. springboot项目发布到独立的tomcat中运行&打成jar包运行

    springboot的打包方式依赖于插件:(下面插件打出的包与普通的包目录结构有区别) <plugin> <groupId>org.springframework.boot&l ...