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. Debian/Ubuntu 下网易云音乐无法由图标/列表 打开的解决方案

    前言 本文为使用图标或快捷方式直接打开网易云音乐的一个从安装说起的到解决问题的简单教程 环境 debian 9.5 理论上使用apt包管理器的发行版均支持 源使用国内的源即可,无需特殊指定 安装网易云 ...

  2. MAC洪水攻击

    MAC洪水攻击原理 传统的交换机在数据转发过程中依靠对CAM表的查询来确定正确的转发接口,一旦在查询过程中无法找到相关的目的MAC对应的条目,此数据帧将作为广播帧来处理,CAM表的容量有限,只能存储不 ...

  3. golang string int int64转换

    #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, 6 ...

  4. jsp中的JSTL与EL表达式用法

    JSTL (JSP Standard Tag Library ,JSP标准标签库) JSTL标签库分为5类:JSTL核心标签库.JSTL函数标签库.数据库标签库.I18N格式化标签库.XML标签库. ...

  5. C# 批量修改文件名

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. Ubuntu16.04搭建QingdaoU(docker一键式部署)

    QDUOJ已经开源到2.0版本了,下面的教程不再适用,仅做纪念吧! 这几天装什么Linux.开源OJ上瘾了...竟然没去刷题...嗯,做好记录就写题啦! 先上原始网站的图: 风格不错,很符合我的口味. ...

  7. shell 终端常用插件

    参考链接: http://get.ftqq.com/992.get 1.zsh 2.autojump 3.apt-get install lamp-server^ 4.tldr 5.tree (显示目 ...

  8. HTML5实现全屏API【进入和退出全屏】

    现在主流浏览器基本上实现了全屏效果,但是不同浏览器实现不一样: [进入和退出全屏] // Webkit (works in Safari5.1 and Chrome 15)element.webkit ...

  9. jquery选择器最后一个,倒数第二个元素

    <div> <p>1</p> <p>2</p> <p>3</p> <p>4</p> < ...

  10. 终端命令行开启和关闭mac隐藏文件

    defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.apple.finde ...