Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP
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
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的更多相关文章
- Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...
- 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 ...
随机推荐
- VMware Workstation 14 UEFI启动
1.新建虚拟机 完成后不要启动 修改虚拟机目录下的XXX.vmx文件 添加一行:firmware="efi" 然后启动UEFI安装系统.
- nginx 配置虚拟机 支持pathinfo
server { server_name shopx.local *.shopx.local; charset utf-8; root /Users/x/www/php/shopx.local/sho ...
- 第四天,for循环,格式化输出,占位符,pycharm安装.列表处理
字符格式化输出 占位符 %s s = string %d d = digit 整数 %f f = float 浮点数,约等于小数 列表,元组 查 索引(下标) ,都是从0开始 切片 .count 查某 ...
- Django 1.8.11 REST风格路由
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community b ...
- POJ 1904:King's Quest【tarjan】
题目大意:给出一个二分图的完美匹配(王子和公主的烧死名单表),二分图x部和y部均只有n个点,问对于每一个x部的点,他能选择哪些点与之匹配 使得与之匹配后,剩余图的最大匹配仍然是n 思路:这题是大白书3 ...
- IPython的常见用法
IPython :交互式的Python命令行 安装: pip install ipython 使用(命令行中启动): ipython # 与Python解释器的使用方法一致 IPython高级功能: ...
- [转]使用fdisk磁盘分区和 Linux 文件系统
概述 在本文中,学习磁盘分区和 Linux 文件系统相关内容.学习: 创建分区 使用 mkfs 命令来设置 ext2.ext3.ext4.xfs.Reiser v3 和 vfat 文件系统 创建和管理 ...
- Java设计模式之(工厂模式)
工厂模式: 工厂模式可以分为三类: 1)简单工厂模式(Simple Factory) 2)工厂方法模式(Factory Method) 3)抽象工厂模式(Abstract Factory) 简单工厂模 ...
- Python---django轻量级框架
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- PLSQL安装资料
一.plsql developer 注册码 plsql developer 10 注册码 product code :4v6hkjs66vc944tp74p3e7t4gs6duq4m4szbf3t38 ...