codeforces round #419 E. 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])
复杂度O(n^2).
注意初始化和边界调节:
F[x][0][0]是要赋为0的,因为当前节点不打折时是可以不选的,而F[x][0][1]不能.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=;
int n,m;
int head[N],num=,w[N],h[N],now[N];ll F[N][N][];
struct Lin
{
int next,to;
} a[N<<];
void init(int x,int y)
{
a[++num].next=head[x];
a[num].to=y;
head[x]=num;
}
void dfs(int x)
{
int u;
now[x]=;
F[x][][]=;
F[x][][]=w[x];F[x][][]=h[x];
for(int i=head[x]; i; i=a[i].next)
{
u=a[i].to;
dfs(u);
for(int j=now[x];j>=;j--)
{
for(int k=; k<=now[u]; k++)
{
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
F[x][j+k][]=min(F[x][j+k][],F[u][k][]+F[x][j][]);
}
}
now[x]+=now[u];//now为当前节点的子节点个数.
}
}
int main()
{
scanf("%d%d",&n,&m);
int x,y,fa;
scanf("%d%d",&w[],&h[]);
h[]=w[]-h[];
memset(F,/,sizeof(F));
for(int i=; i<=n; i++)
{
scanf("%d%d%d",&x,&y,&fa);
w[i]=x;
h[i]=x-y;
init(fa,i);
}
dfs();
for(int i=n; i>=; i--)
if(F[][i][]<=m || F[][i][]<=m)
{
printf("%d",i);
break;
}
return ;
}
codeforces round #419 E. Karen and Supermarket的更多相关文章
- Codeforces Round #419 D. Karen and Test
Karen has just arrived at school, and she has a math test today! The test is about basic addition an ...
- codeforces round #419 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 B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- codeforces round #419 A. Karen and Morning
Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As yo ...
- 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) A-E
上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...
- Codeforces round 419 div2 补题 CF 816 A-E
A Karen and Morning 水题 注意进位即可 #include<bits/stdc++.h> using namespace std; typedef long long i ...
- Codeforces Round #419
A Karen and Morning 找最近的回文时间 模拟 往后推 判判就行 //By SiriusRen #include <bits/stdc++.h> using namesp ...
- Codeforces Round #419 (Div. 1) (ABCD)
1. 815A Karen and Game 大意: 给定$nm$矩阵, 每次选择一行或一列全部减$1$, 求最少次数使得矩阵全$0$ 贪心, $n>m$时每次取一列, 否则取一行 #inclu ...
随机推荐
- 听翁恺老师mooc笔记(9)--枚举
枚举类型的定义 用符号而不是具体的数字来表示程序中的数字,这么表示的好处是可读性,当别人看你的程序,看到的是单词,很容易理解这些数字背后的含义,那么用什么符号来表示名字哪?需要const int常量的 ...
- C语言总结报告
1.当初你是如何做出选择计算机专业的决定的? 经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 当初报考计算机专业,是看到计算机专业在当今社会有良 ...
- verilog学习笔记(1)_两个小module
第一个小module-ex_module module ex_module( input wire sclk,//声明模块的时候input变量一定是wire变量 input wire rst_n,// ...
- 200行Python代码实现2048
200行Python代码实现2048 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面 ...
- new malloc和delete free 的区别
今天看了一个面试题:问new 和 malloc, delete 和 free 的区别,扭捏了半天,也没说完全:现总结如下: 1.先看看new 和 delete 看一个例子: <span styl ...
- pymysql安装和使用
一.pymysql安装 安装mymysql前请确认python环境已经准备好,在之前的博文http://www.cnblogs.com/newzol/p/8682176.html有说明pythonwe ...
- Spring中报"Could not resolve placeholder"的解决方案
除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderCon ...
- kubernetes入门(04)kubernetes的核心概念(1)
一.ReplicationController/ReplicaSet 在Kubernetes集群中,ReplicationController能够确保在任意时刻,指定数量的Pod副本正在运行.如果Po ...
- xftp上传文件失败,执行程序发现磁盘满了:No space left on device
参考链接 No space left on device 解决Linux系统磁盘空间满的办法http://www.cnblogs.com/aspirant/p/3604801.html如何解决linu ...
- SpringBoot入门:新一代Java模板引擎Thymeleaf(实践)
菜鸟教程:http://www.runoob.com/ http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js http://apps.b ...