Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E
题意:
去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用。问最多能买多少件商品?
思路:
第一件商品使用优惠券不需要前提,别的都是需要的,然后这样就形成了一棵以1为根的树。
这样,很容易想到是树形dp。
d【u】【j】【0/1】表示以u为根的子数中选择j件商品所需的最少花费,0/1表示u商品是否能用优惠券。
解释一下代码中的sz【】,它所代表的是以u为根的子树的结点数。

当我们现在访问的是1号结点时,sz【1】=1,然后访问2号结点,2号结点访问结束后,我们就会重新回到1号结点的dfs处,然后进行状态转移
for(int j=sz[u];j>=;j--) //表示sz【u】中选取的j个个数 for(int k=;k<=sz[v];k++) //表示v结点及其子树中选取的个数
最后将sz【2】的值加到sz【1】中,这样等下一次进行结点3的状态转移时,只需要考虑在2的子树中选取多少个和3的子树中选取多少个即可。到4时,只需要考虑在2和3中一共选取多少个和4的子树中选取多少个,因为前者在上一步已经计算出了最小花费。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n, m; int pa[maxn], pb[maxn];
int sz[maxn];
int d[maxn][maxn][]; vector<int> g[maxn]; void dfs(int u)
{
sz[u]=;
d[u][][]=, d[u][][]=pa[u], d[u][][]=pa[u]-pb[u];
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
dfs(v); for(int j=sz[u];j>=;j--)
{
for(int k=;k<=sz[v];k++)
{
d[u][j+k][]=min(d[u][j+k][],d[u][j][]+d[v][k][]);
d[u][j+k][]=min(d[u][j+k][],min(d[u][j][]+d[v][k][],d[u][j][]+d[v][k][]));
}
}
sz[u]+=sz[v];
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n, &m))
{
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<=n;i++)
{
scanf("%d%d",&pa[i],&pb[i]);
if(i>)
{
int x;
scanf("%d",&x);
g[x].push_back(i);
}
} memset(d,INF,sizeof(d));
dfs(); int i;
for(i=;i<=n;i++)
{
if(min(d[][i][],d[][i][])>m) break;
}
printf("%d\n",i-);
}
return ;
}
Codeforces Round #419 (Div. 2) E. 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 ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)
http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...
- Codeforces Round #419 (Div. 2) C. Karen and Game
C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)
http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...
- 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test
打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...
- 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game
容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...
- Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp
题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...
- Codeforces Round #382 (Div. 2) 继续python作死 含树形DP
A - Ostap and Grasshopper zz题能不能跳到 每次只能跳K步 不能跳到# 问能不能T-G 随便跳跳就可以了 第一次居然跳越界0.0 傻子哦 WA1 n,k = map ...
随机推荐
- android 仿微信聊天界面,以及语音录制功能
extends:http://104zz.iteye.com/blog/1709840 本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图: 第一:chat.xml设计 ...
- mysql max_allowed_packet参数值改大后,莫名被还原
mysql数据库用innodb引擎,mysql max_allowed_packet在my.cnf中值加大后,够一段时间,系统会莫名把这个参数的值改小. innodb_buffer_pool_size ...
- thinkphp --- 写入日志
在开发过程中,对于一些参数,不好直接输入或者打印调试,特别是在微信开发过程中,这个时候,通过日志来查看信息就显得格外重要. 下面是在TP3.2.3框架中,写入日志的方法: public functio ...
- IIS7设置IP地址和域名限制
在IIS中可以通过IP地址域名设置来控制拒绝或允许特定范围内的IP对网站的访问权限,下面简单介绍如何在IIS7.5中设置,如下图,是IIS7.5的主界面 一.安装“IP地址和域限制”功能 选定一个网站 ...
- vue报错 vue-cli 引入 stylus 失败
1.1.1. vue-cli 引入 stylus 失败 先通过vue-cli的webpack模板建立文件夹: vue init webpack test-stylus 然后安装依赖 npm ins ...
- Windows安装使用git
下载安装Windows安装文档Git-2.16.2-64-bit双击安装(安装过程不详述) 打开git客户端 新建代码命令 mkdir /c/code 进入该目录(对应windows的c盘下面的目录) ...
- HotSpot VM
1.4.2 Sun HotSpot VM_深入理解Java虚拟机:JVM高级特性与最佳实践(第2版)_红黑联盟读书频道 http://book.2cto.com/201306/25434.html 提 ...
- php引用(&)详解及注意事项——引用返回function &a();&a()
http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/09/10/2173092.html 函数的引用返回 先看代码 <?php func ...
- Jury Compromise---poj1015(动态规划,dp,)
题目链接:http://poj.org/problem?id=1015 大致题意: 在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n 个人作为陪审团的候 ...
- Bungee Jumping---hdu1155(物理题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1155 题目很长,但是很容易理解,就是人从高s的桥上跳下来,手拉着长为l的绳子末端,如果绳子太短那么人将 ...