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 lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.
The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good can only be bought once.
Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by di. Of course, a coupon cannot be used without buying the corresponding good.
There is, however, a constraint with the coupons. For all i ≥ 2, in order to use the i-th coupon, Karen must also use the xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).
Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?
The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively.
The next n lines describe the items. Specifically:
- The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively.
- If i ≥ 2, this is followed by another integer, xi (1 ≤ xi < i), denoting that the xi-th coupon must also be used before this coupon can be used.
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
- 6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
- 4
- 5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
- 5
In the first test case, Karen can purchase the following 4 items:
- Use the first coupon to buy the first item for 10 - 9 = 1 dollar.
- Use the third coupon to buy the third item for 12 - 2 = 10 dollars.
- Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars.
- Buy the sixth item for 2 dollars.
The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.
In the second test case, Karen has enough money to use all the coupons and purchase everything.
树上背包,F[x][j][0/1]表示x子节点和本身中,选j个,当前节点是否打折(0/1)
方程式:
F[x][j+k][0]=min(F[x][j+k][0],F[u][k][0]+F[x][j][0])
F[x][j+k][1]=min(F[x][j+k][1],F[u][k][1]+F[x][j][1])
F[x][j+k][1]=min(F[x][j+k][1],F[u][k][0]+F[x][j][1])
注意初始化和边界调节:
F[x][0][0]是要赋为0的,因为当前节点不打折时是可以不选的,而F[x][0][1]不能.
虽然是n^3但能过,就没必要打多叉树转二叉树
转载自YZH神犇%%%%OTTTTTZ
http://www.cnblogs.com/Yuzao/p/7074373.html
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- typedef long long lol;
- struct Node
- {
- int next,to;
- }edge[];
- int head[],num,n,size[];
- lol b,w[],d[],v[],f[][][];
- void add(int u,int v)
- {
- num++;
- edge[num].next=head[u];
- head[u]=num;
- edge[num].to=v;
- }
- void dfs(int x)
- {int i,j,k;
- f[x][][]=;
- f[x][][]=v[x];
- f[x][][]=w[x];
- size[x]=;
- for (i=head[x];i;i=edge[i].next)
- {
- int v=edge[i].to;
- dfs(v);
- for (j=size[x];j>=;j--)
- {
- for (k=;k<=size[v];k++)
- {
- f[x][j+k][]=min(f[x][j+k][],f[x][j][]+f[v][k][]);
- f[x][j+k][]=min(f[x][j+k][],f[v][k][]+f[x][j][]);
- f[x][j+k][]=min(f[x][j+k][],f[v][k][]+f[x][j][]);
- }
- }
- size[x]+=size[v];
- }
- }
- int main()
- {int i,fa;
- cin>>n>>b;
- scanf("%I64d%I64d",&w[],&d[]);
- v[]=w[]-d[];
- for (i=;i<=n;i++)
- {
- scanf("%I64d%I64d%d",&w[i],&d[i],&fa);
- v[i]=w[i]-d[i];
- add(fa,i);
- }
- memset(f,/,sizeof(f));
- dfs();
- for (i=n;i>=;i--)
- if (f[][i][]<=b||f[][i][]<=b)
- {
- cout<<i<<endl;
- return ;
- }
- }
codeforces 815C Karen and Supermarket的更多相关文章
- Codeforces 815C Karen and Supermarket 树形dp
Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...
- Codeforces 815C. Karen and Supermarket【树形DP】
LINK 思路 首先发现依赖关系是一个树形的结构 然后因为直接算花多少钱来统计贡献不是很好 因为数组开不下 那就可以算一个子树里面选多少个的最小代价就可以了 注意统计贡献的时候用优惠券的答案只能在1号 ...
- CodeForces 816E Karen and Supermarket ——(树形DP)
题意:有n件商品,每件商品都最多只能被买一次,且有一个原价和一个如果使用优惠券以后可以减少的价格,同时,除了第一件商品以外每件商品都有一个xi属性,表示买这个商品时如果要使用优惠券必须已经使用了xi的 ...
- 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 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- CF815C Karen and Supermarket
题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...
- CF815C Karen and Supermarket [树形DP]
题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...
- E. Karen and Supermarket
E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- 【Codeforces 815C】Karen and Supermarket
Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...
随机推荐
- Beta冲刺NO.5
Beta冲刺 第五天 1. 昨天的困难 1.昨天的困难主要是在类的整理上,一些逻辑理不清,也有一些类写的太绝对了,扩展性就不那么好了,所以,昨天的困难就是在重构上. 页面结构太凌乱,之前没有统筹好具体 ...
- js计时功能
//个位秒加 function time4jia() { //分钟60为上限 所有加停止 if (sz(a('time1').innerHTML) == 6) { return; } var m4 = ...
- python学习笔记-问题
1.字典按照值进行排序输出 2.返回函数-闭包的使用
- 卡尔曼滤波法C编程
float Angle = 0.0;//卡尔曼滤波器的输出值,最优估计的角度 //float Gyro_x = 0.0;//卡尔曼滤波器的输出值,最优估计的角速度 float Q_angle = 0. ...
- 更优雅的方式: JavaScript 中顺序执行异步函数
火于异步 1995年,当时最流行的浏览器--网景中开始运行 JavaScript (最初称为 LiveScript). 1996年,微软发布了 JScript 兼容 JavaScript.随着网景.微 ...
- TF中conv2d和kernel_initializer方法
conv2d中的padding 在使用TF搭建CNN的过程中,卷积的操作如下 convolution = tf.nn.conv2d(X, filters, strides=[1,2,2,1], pad ...
- linux 下 /bin /sbin 的区别
/bin,/sbin,/usr/bin,/usr/sbin区别 / : this is root directory root 用户根目录 /bin : command ...
- Eclipse在线更新慢
一.去掉不必要的更新 打开Windows-Preferences -> Install/Update –> Available Software Sites,将不需要的更新停用 二.关闭自 ...
- python爬虫requests的使用
1 发送get请求获取页面 import requests # 1 要爬取的页面地址 url = 'http://www.baidu.com' # 2 发送get请求 拿到响应 response = ...
- python Django注册页面显示头像
python Django注册页面显示头像(views) def register(request): ''' 注册 :param request: :return: ''' if request.m ...