Codeforces815C 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?
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
input
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
output
5
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个商品,每个的价值为ci,每个商品有自己的优惠券,可以优惠di元。除开第一个优惠券,每个优惠券的使用都依赖于前面的某个优惠券(xi)。问我们用b元最多能买多少个商品。
这道题可以说是HDU1561的衍生,都是树上有依赖的背包问题。而HDU1561的依赖并不是指优惠券的依赖,而是指商品与商品的依赖,也就是说对于商品v必须买了商品u才能买商品v。
他的转移方程是这样的:

for(int j=size[u];j>=1;j--)//这里的size[u]不包括size[v]
for(int k=1;k<=size[v];k++)
dp[u][j+k]=max(dp[u][j+k],dp[u][j]+dp[v][k]);

这里的dp[u][j+k]表示以u为根节点的子树在选择了j+k个节点后的最优答案。
我们考虑怎样处理优惠券的有关问题。
我们将上面的dp数组加上半维,也就是
dp[u][j+k][0/1]表示以u为根基点的子树选择了j+k个节点,u这个点的优惠券不用/用后的最优答案。
对于初值,我们不难发现:

dp[u][0][0]=0;
dp[u][1][0]=c[u];
dp[u][1][1]=c[u]-d[u];

其余的赋+inf。
那么怎么转移呢?
显然我们当前的dp[u][j+k][0/1]的状态要从他的子树们转移过来,而他的子树能否使用优惠券实际上由u这个点决定。
那么无非就是u使用优惠券与否的问题。
如果u不使用优惠券,其子树的每个节点自然也不能使用,也就是说从dp[u][j][0]+dp[v][k][0]这个答案转移过来。
如果u使用优惠券,那么对于v及其子树就有两种决断,一种是v使用优惠券,一种是v不使用优惠券,也就是说从dp[u][j][1]+dp[v][k][1];dp[u][j][1]+dp[v][k][0]两个答案转移过来。
至于最后的答案,我们在dp[1][n~1][0/1]里面找第一个小于等于b的答案的下标就行了。
(据说这道题的时间复杂度可以证明是O(N^2)的)
代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5000,inf=0x3f3f3f3f;
struct edge
{
    int v,next;
}ed[N+5];
int head[N+5],num;
int n,b,ans;
int c[N+5],d[N+5],x[N+5],w[N+5];
int dp[N+5][N+5][2],size[N+5];
void build(int u,int v)
{
    ed[++num].v=v;
    ed[num].next=head[u];
    head[u]=num;
}
void dfs(int u)
{
    size[u]=1;
    dp[u][0][0]=0;
    dp[u][1][0]=c[u];
    dp[u][1][1]=c[u]-d[u];
    for(int i=head[u];i!=-1;i=ed[i].next)
    {
        int v=ed[i].v;
        dfs(v);
        for(int j=size[u];j>=0;j--)
        for(int k=0;k<=size[v];k++)//合并v的子树的答案和u已经便利到的子树中除开v的所有子树的答案 
        dp[u][j+k][0]=min(dp[u][j+k][0],dp[u][j][0]+dp[v][k][0]),
        dp[u][j+k][1]=min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][1]),
        dp[u][j+k][1]=min(dp[u][j+k][1],dp[u][j][1]+dp[v][k][0]);
        size[u]+=size[v];
    }
}
int main()
{
    while(~scanf("%d%d",&n,&b))
    {
        memset(head,-1,sizeof(head));num=0;
        memset(dp,inf,sizeof(dp));
        scanf("%d%d",&c[1],&d[1]);
        for(int i=2;i<=n;i++)
        {
            int u;
            scanf("%d%d%d",&c[i],&d[i],&u);
            build(u,i); 
        }
        dfs(1);
        ans=n;
        while(dp[1][ans][0]>b&&dp[1][ans][1]>b)ans--;
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值