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

事实上,她只花了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. PAT天梯赛练习题——L3-004. 肿瘤诊断(三维连通块并查集)

    L3-004. 肿瘤诊断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶 ...

  2. BZOJ-1507 文本编辑器(Editor)

    一道极其相似的题...http://hi.baidu.com/8361101/item/5b149103cbf4007cbee97e5f 就多了个区间查找,少了个翻转... 少了翻转的话貌似可以不用S ...

  3. vue项目实战, webpack 配置流程记录

    vue项目实战记录,地址在这 购物车单界面 npm install npm run dev 跑起来可以看到界面效果 这里简单记录一下webpack的编译流程 入口 package.json " ...

  4. 征途(bzoj 4518)

    Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜 ...

  5. android开发里跳过的坑——camera调用setDisplayOrientation设置预览显示旋转无效

    问题原因,在surfaceview没有设置给camera之前调用了,所以,这个方法一定要在camera.setPreviewDisplay(surfaceHolder)这个之后,启动相机预览之前调用.

  6. tyvj 1061 Mobile Service

    线性DP 本题的阶段很明显,就是完成了几个请求,但是信息不足以转移,我们还需要存储三个服务员的位置,但是三个人都存的话会T,我们发现在阶段 i 处,一定有一个服务员在 p[i] 处,所以我们可以只存另 ...

  7. bigdata related

    hive: http://lxw1234.com/archives/2015/07/413.htm 搜狗实验室数据集: https://www.sogou.com/labs/resource/list ...

  8. LinkedList类的基本方法的用法

    package cn.zmh.LinkedList; import java.util.Iterator; import java.util.LinkedList; public class Link ...

  9. mysql delete语句不能用别名

    在mysql数据库里运行delete语句 delete ’; 发现会报错: [Err] - You have an error in your SQL syntax; check the manual ...

  10. 5.Longest Palindrome substring

    /* * 5.Longest Palindrome substring * 2016-4-9 by Mingyang 自然而然的想到用dp来做 * 刚开始自己做的时候分的条件太细,两个index相等, ...