C. 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
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
output
4
 
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.

 题意:

  给你n件商品,

  给出每件商品的 原价和 优惠券,用优惠券前必须使用第x件的优惠券才可以使用当前优惠券

  求b元下,最多买多少件商品

题解:

  暴力转移

  dp[now][x][0/1]表示当前节点now,买x件商品,当前now节点的商品是否使用优惠券的最小花费

  最后check一下就行

  先转移后更新是个优化

#include<bits/stdc++.h>
using namespace std;
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const int N = 5e3+, mod = , inf = 2e9; int n,b,c[N],d[N],siz[N],x[N];
LL dp[N][N][];
vector<int > G[N];
void dfs(int u,int f) {
siz[u] = ;
dp[u][][] = ;
dp[u][][] = c[u];
dp[u][][] = c[u] - d[u];
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == f) continue;
dfs(to,u);
for(int j = siz[u]; j >= ; --j) {
for(int k = siz[to]; k >= ; --k) {
dp[u][j+k][] = min(dp[u][j+k][], dp[u][j][] + dp[to][k][]);
dp[u][j+k][] = min(dp[u][j+k][], dp[u][j][] + min(dp[to][k][],dp[to][k][]));
}
}
siz[u] += siz[to];
}
}
int main() {
scanf("%d%d",&n,&b);
scanf("%d%d",&c[],&d[]);
for(int i = ; i <= n; ++i) {
scanf("%d%d%d",&c[i],&d[i],&x[i]);
G[x[i]].push_back(i);
}
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j) {
dp[i][j][] = INF;
dp[i][j][] = INF;
}
dfs(,);
int ans = ;
for(int i = ; i <= n; ++i)
if(min(dp[][i][],dp[][i][]) <= b) ans = i;
printf("%d\n",ans);
return ;
}

Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP的更多相关文章

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

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

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

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

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

  5. Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)

    http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...

  6. 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test

    打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...

  7. 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game

    容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...

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

  9. Codeforces Round #382 (Div. 2) 继续python作死 含树形DP

    A - Ostap and Grasshopper zz题能不能跳到  每次只能跳K步 不能跳到# 问能不能T-G  随便跳跳就可以了  第一次居然跳越界0.0  傻子哦  WA1 n,k = map ...

随机推荐

  1. RHEL6.5 DHCP服务器搭建

    RHEL6.5 DHCP服务器搭建: DHCP服务器是用来分配给其它客户端IP地址用的,在RHEL 6.5中DHCP服务器搭建方法如下: 第一步,通过yum安装dhcp服务: 命令:yum insta ...

  2. 树莓派 - 蓝牙 (1) 试试Beacon

    首先先了解一下bluez, 以及常用的tools. - hcitool.bluetoothctl等工具,可以进行BLE设备的扫描.连接.配对.广播等操作: - hcitool可以发送HCI comma ...

  3. Specified VM install not found: type Standard VM, name JDK1.8

    真正的问题解决方法在这里:在项目中,右键点击ant文件,选择Run As -- External Tools Configuration,在这个页面的顶端就会看到有红叉叉的报错,报错信息就是Speci ...

  4. 【实验吧】Just Click

    拿到答案需要正确地点击按钮 格式:simCTF{ } 解题链接: http://ctf5.shiyanbar.com/re/rev4.exe 由于最近在学数据库是c#编程,发现是c#,于是用.net ...

  5. mysql 常用命令(一)

    1. 函数向日期添加指定的时间间隔 DATE_ADD(date,INTERVAL expr type)eg:DATE_ADD(CURDATE(),INTERVAL 1 MONTH) //在当前时间加一 ...

  6. 04004_使用JavaScript完成注册表单数据校验

    1.需求分析 (1)用户在进行注册的时候会输入一些内容,但是有些用户会输入一些不合法的内容,这样会导致服务器的压力过大,此时我们需要对用户输入的内容进行一个校验(前端校验和后台校验): (2)前端校验 ...

  7. javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法

    下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...

  8. 【模板】prim的heap优化

    简单的代码.. 时间复杂度为O((n + m)logn) 大部分情况下还是跑不过kruskal的,慎用. #include <cstdio> #include <queue> ...

  9. BZOJ1744: [Usaco2005 oct]Skiing 奶牛滑雪

    n<=100 * m<=100的地图,每个数绝对值不超过25,从1,1到n,m,一开始速度v,从数字A走到数字B速度会变成v*2^(A-B),求到终点最短时间. 可以发现,相同的数字出发的 ...

  10. hdu2294:Pendant

    T<=10组数据问K<=30种珠子每种n<=1e9串成1~n长度的序列共有多少种,mod1234567891. 方程没想到.矩阵不会推.很好. f[i][j]--长度i,j种珠子方案 ...