在回家的路上,凯伦决定到超市停下来买一些杂货。 她需要买很多东西,但因为她是学生,所以她的预算仍然很有限。

事实上,她只花了b美元。

超市出售N种商品。第i件商品可以以ci美元的价格购买。当然,每件商品只能买一次。

最近,超市一直在努力促销。凯伦作为一个忠实的客户,收到了n张优惠券。

如果凯伦购买i次商品,她可以用i次优惠券降低di美元。 当然,不买对应的商品,优惠券不能使用。

然而,对于优惠券有一个规则。对于所有i>=2,为了使用i张优惠券,凯伦必须买第j个商品。

凯伦想知道。她能在不超过预算B的情况下购买的最大商品数量是多少?

输入输出样例

输入样例#1: 复制

6 16

10 9

10 5 1

12 2 1

20 18 3

10 2 3

2 1 5

输出样例#1: 复制

4

Solution

考试题目写挂,看错题了。想看原题的戳这里。树型dp,我们定义\(f[i][j][2]\)代表i结点选了j个节点,当前节点选不选。容易想到dp方程为

\[f[x][k+j][0]=min(f[x][k+j][0],f[to][j][0]+f[x][k][0]);
\]

\[f[x][k+j][0]=min(f[x][k+j][0],f[to][j][1]+f[x][k][0]);
\]

\[f[x][k+j][1]=min(f[x][k+j][1],f[to][j][1]-v[to]+f[x][k][1]);
\]

\[f[x][k+j][1]=min(f[x][k+j][1],f[to][j][1]+f[x][k][1]);
\]

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int to,next;
}a[1000100];
int w[101000],v[100100],len,last[101010],son[100010],tot;
int f[5100][5100][2];
void add(int a1,int a2)
{
a[++len].to=a2;
a[len].next=last[a1];
last[a1]=len;
}
void dp(int x,int father)
{
son[x]=1;
for(int i=last[x];i;i=a[i].next)
{
int to=a[i].to;
if(to==father)
continue;
dp(to,x);
for(int k=son[x];k>=0;k--)
for(int j=son[to];j>=0;j--)
{
f[x][k+j][0]=min(f[x][k+j][0],f[to][j][0]+f[x][k][0]);
f[x][k+j][0]=min(f[x][k+j][0],f[to][j][1]+f[x][k][0]);
f[x][k+j][1]=min(f[x][k+j][1],f[to][j][1]-v[to]+f[x][k][1]);
f[x][k+j][1]=min(f[x][k+j][1],f[to][j][0]+f[x][k][1]);
}
son[x]+=son[to];
}
}
int main()
{
//freopen("shopping.in","r",stdin);
//freopen("shopping.out","w",stdout);
memset(f,0x3f,sizeof(f));
int n,s,x;
cin>>n>>s;
cin>>w[1]>>v[1];
w[1]-=v[1];
f[1][0][0]=0;f[1][1][1]=w[1];
for(int i=2;i<=n;i++)
{
scanf("%d%d%d",&w[i],&v[i],&x);
add(x,i);add(i,x);
}
for(int i=2;i<=n;i++)
f[i][0][0]=0,f[i][1][1]=w[i];
dp(1,0);
for(int i=n;i>=0;i--)
{
if(f[1][i][0]<=s||f[1][i][1]<=s)
{cout<<i;return 0;}
}
}

博主蒟蒻,可以随意转载,但必须附上原文链接k-z-j

[原创] Karen and Supermarket 2的更多相关文章

  1. Codeforces 815C Karen and Supermarket 树形dp

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

  2. CF815C Karen and Supermarket

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

  3. CF815C Karen and Supermarket [树形DP]

    题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...

  4. E. Karen and Supermarket

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

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

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

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

  8. Codeforces 815 C Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  9. 【Codeforces 815C】Karen and Supermarket

    Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...

随机推荐

  1. Android单个按钮自定义Dialog

    代码改变世界 Android单个按钮自定义Dialog dialog_layout.xml <?xml version="1.0" encoding="utf-8& ...

  2. 数据结构1 「在线段树中查询一个区间的复杂度为 $O(\log N)$」的证明

    线段树属于二叉树, 其核心特征就是支持区间加法,这样就可以把任意待查询的区间$[L, R]$分解到线段树的节点上去,再把这些节点的信息合并起来从而得到区间$[L,R]$的信息. 下面证明在线段树上查询 ...

  3. BZOJ 3850: ZCC Loves Codefires【贪心】

    Though ZCC has many Fans, ZCC himself is a crazy Fan of a coder, called "Memset137". It wa ...

  4. ObjectDataSource配合存储过程(采用数据集)的使用(删除可以解决,但是编辑出错好像它的方法也无法解决

    原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] ObjectDataSource是比较有意思的一个东西 通过在网络上遍访各位高手,终于自己有了一些心得体会。现总结 ...

  5. hdu 5691 Sitting in Line

    传送门 Sitting in Line Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/O ...

  6. EGO V2

    Original EGO: mkdir -p ~/Library/Developer/Xcode/UserData/FontAndColorThemes; cd ~/Library/Developer ...

  7. druid 的应用(密码加密),logback的应用

    参考博客:https://github.com/yjmyzz/spring-mybatis-multidatasourcehttp://www.cnblogs.com/dream-to-pku/p/6 ...

  8. 读取编码器信息Python2.7和Python3.3版本差异及解决,一次订阅多次调用callback的解决

    1. Python3.3以字节类型返回编码器信息,b'...',BUF: b'\xc3KOO\x00OO\x00OO\x00OO\x00OO\x00\x03\x00\x00\x00\x00\x99R\ ...

  9. iOS 读书笔记 第一章

    1.确定某个实例或类方法是否可用. 1)使用NSObject的类方法instancesRespondToSelector:来确定是否在该类的一个实例中存在一个特定的选择器. NSArray *arra ...

  10. 【深入Java虚拟机】之五:多态性实现机制——静态分派与动态分派

    方法解析 Class文件的编译过程中不包含传统编译中的连接步骤,一切方法调用在Class文件里面存储的都只是符号引用,而不是方法在实际运行时内存布局中的入口地址.这个特性给Java带来了更强大的动态扩 ...