题目传送门

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?

Input

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

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

Examples
input

Copy
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
output

Copy
4
input

Copy
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
output

Copy
5
Note

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.


  分析:

  考试的时候遇到这道题结果还是一脸懵逼,果然我$DP$还是太弱了,还是得好好补补。

  考虑用树形$DP$,根据题意构建出一棵树,然后从根节点开始深搜,把每个节点遍历一遍,然后从叶子节点开始向根节点转移状态。

  定义$f[x][j][0/1]$表示遍历到了第$x$个商品时已经购买了$j$个商品,$0/1$表示第$x$个商品是否打折。

  那么状态转移方程为:

  $f[x][j+k][0]=Min(f[x][j+k][0],f[x][j][0]+f[y][k][0]);$

  $f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][0]);$

  $f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][1]);$

  其中$j$和$k$分别表示当前节点和其子节点已经搜过的子树中购买的商品个数。可能有点绕,可以结合代码理解。

  Code:

//It is made by HolseLee on 15th Aug 2018
//CF815C
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Max(a,b) (a)>(b)?(a):(b)
#define Min(a,b) (a)<(b)?(a):(b)
#define Swap(a,b) (a)^=(b)^=(a)^=(b)
using namespace std; const int N=;
int n,m,head[N],siz,w[N],c[N],rt[N];
long long f[N][N][];
struct Node{
int to,nxt;
}edge[N<<]; inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){
if(ch=='-')flag=true;ch=getchar();
}
while(ch>=''&&ch<=''){
num=num*+ch-'';ch=getchar();
}
return flag?-num:num;
} inline void add(int x,int y)
{
edge[++siz].to=y;
edge[siz].nxt=head[x];
head[x]=siz;
} void dfs(int x)
{
rt[x]=;f[x][][]=;
f[x][][]=w[x];f[x][][]=c[x];
for(int i=head[x];i;i=edge[i].nxt){
int y=edge[i].to;
dfs(y);
for(int j=rt[x];j>=;--j)
for(int k=;k<=rt[y];++k){
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
f[x][j+k][]=Min(f[x][j+k][],f[x][j][]+f[y][k][]);
}
rt[x]+=rt[y];
}
} int main()
{
//freopen("shopping.in","r",stdin);
//freopen("shopping.out","w",stdout);
n=read();m=read();
memset(f,/,sizeof(f));
w[]=read();c[]=read();c[]=w[]-c[];
int x,y,z;
for(int i=;i<=n;++i){
x=read();y=read();z=read();
w[i]=x;c[i]=x-y;
add(z,i);
}
dfs();
for(int i=n;i>=;--i){
if(f[][i][]<=m||f[][i][]<=m){
printf("%d\n",i);
break;
}
}
return ;
}

CF815C Karen and Supermarket [树形DP]的更多相关文章

  1. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

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

  3. 816E. Karen and Supermarket 树形DP

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

  4. CF815C Karen and Supermarket

    题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...

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

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

  6. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  7. codeforces 816 E. Karen and Supermarket(树形dp)

    题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...

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

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

  9. E. Karen and Supermarket

    E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. java中16进制转换10进制

    java中16进制转换10进制 public static void main(String[] args) { String str = "04e1"; String myStr ...

  2. 关于IE浏览器输入框字体不兼容问题

    当写完 input的text输入框后 IE里输入框字体偏上 那么有个简单的解决办法 就是设置行高line-height:Xpx\9;就可以解决   \9表示所有IE

  3. 【BZOJ】1036 [ZJOI2008]树的统计Count

    [算法]树链剖分+线段树 [题解]模板题,见http://www.cnblogs.com/onioncyc/p/6207462.html 调用线段数时要用新编号pos[i] !!! #include& ...

  4. HTTP/2.0 简单总结(转载)

    HTTP/2.0 简单总结(转载于https://linjunzhu.github.io/blog/2016/03/10/http2-zongjie/) 如何使用上 HTTP/2.0 需要浏览器的支持 ...

  5. 用C++写一个没人用的ECS

    github地址:https://github.com/yangrc1234/Resecs 在做大作业的时候自己实现了一个简单的ECS,起了个名字叫Resecs. 这里提一下一些实现的细节,作为回顾. ...

  6. 学习Python函数笔记之二(内置函数)

    ---恢复内容开始--- 1.内置函数:取绝对值函数abs() 2.内置函数:取最大值max(),取最小值min() 3.内置函数:len()是获取序列的长度 4.内置函数:divmod(x,y),返 ...

  7. Test plan

    Options for Test Strategy: 1. Regular test: all the planned test cases will be executed 2. Extented ...

  8. hdfs的datanode工作原理

    datanode的作用: (1)提供真实文件数据的存储服务. (2)文件块(block):最基本的存储单位.对于文件内容而言,一个文件的长度大小是size,那么从文件的0偏移开始,按照固定的大小,顺序 ...

  9. 摘: 给Shapre命名

    有两种解决方式: 在 VBA 中将slide中的Shape命名,改变shape.name即可. 另外一种方式就是有点投机取巧:你可以点击shap,右键选择web/alternativetext做些标记 ...

  10. linux内存分配方法总结【转】

    转自:http://www.bkjia.com/Linuxjc/443717.html 内存映射结构: 1.32位地址线寻址4G的内存空间,其中0-3G为用户程序所独有,3G-4G为内核占有. 2.s ...