Codeforces 815C Karen and Supermarket 树形dp
感觉就是很普通的树形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的更多相关文章
- 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 ...
- CF815C Karen and Supermarket [树形DP]
题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...
- 816E. Karen and Supermarket 树形DP
LINK 题意:给出n个商品,除第一个商品外,所有商品可以选择使用优惠券,但要求其前驱商品已被购买,问消费k以下能买几个不同的商品 思路:题意很明显就是树形DP.对于一个商品有三种选择,买且使用优惠券 ...
- Codeforces 815C. Karen and Supermarket【树形DP】
LINK 思路 首先发现依赖关系是一个树形的结构 然后因为直接算花多少钱来统计贡献不是很好 因为数组开不下 那就可以算一个子树里面选多少个的最小代价就可以了 注意统计贡献的时候用优惠券的答案只能在1号 ...
- 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 ...
- CodeForces 816E Karen and Supermarket ——(树形DP)
题意:有n件商品,每件商品都最多只能被买一次,且有一个原价和一个如果使用优惠券以后可以减少的价格,同时,除了第一件商品以外每件商品都有一个xi属性,表示买这个商品时如果要使用优惠券必须已经使用了xi的 ...
- [CF816E] Karen and Supermarket1 [树形dp]
传送门 - > \(CF816E\) Karen and Supermarket 题意翻译 在回家的路上,凯伦决定到超市停下来买一些杂货. 她需要买很多东西,但因为她是学生,所以她的预算仍然很有 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- codeforces 337D Book of Evil (树形dp)
题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...
随机推荐
- AnimateWindow类
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 三、u-boot 的配置-mkconfig 脚本
3.1 mkconfig 脚本 100ask24x0_config : unconfig @$(MKCONFIG) $(@:_config=) arm arm920t 100ask24x0 NULL ...
- js 将很长的内容进行页面分页显示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- luogu P4568 [JLOI2011]飞行路线
传送门 看到免费次数\(k\)最多只有10,可以考虑构建\(k+1\)层的分层图,即每一层正常连边,上下两层对应点连边权为0的单向边,最后对所有层里面的\(di_t\)取\(\max\)救星了 #in ...
- 利用QT、QWebview、ffmpeg实现的屏幕录制方案
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- SpringBoot集成Spring Security(授权与认证)
⒈添加starter依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...
- freeRTOS中文实用教程6--错误排查
1.前言 本章主要是为刚接触FreeRTOS 的用户指出那些新手通常容易遇到的问题.这里把最主要的篇幅放在栈溢出以及栈溢出侦测上 2.printf-stdarg.c 当调用标准C 库函数时,栈空间使用 ...
- uboot中的快捷菜单的制作说明 【转】
转自:http://blog.chinaunix.net/uid-22030783-id-366971.html 在uboot中加入快捷操作菜单的方法非常简单,在论坛发布的uboot201003V ...
- Bootstrap3.0学习第四轮(排版)
详情请查看http://aehyok.com/Blog/Detail/10.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- HDU 3579
标准同余方程组,只是在求出值后如果为0,应该输出Mi的Lcm: #include<iostream> #include<cstdio> #include<cstring& ...