Codeforces 815 C 树形依赖背包 解题报告

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

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

【解题报告】
题意:
商店里有n个物品,第i个物品的价格为ci元。每个物品只能买一次。商店发行了n张优惠券,每个物品各有一张优惠卷。如果使用了第i张优惠券,可以使该物品便宜di元钱,必须买商品才能够使用相对应的优惠券。第1张优惠卷可以无条件使用,但对于第i>=2张优惠卷,如果需要使用第i张优惠券,则必须先使用xi这张优惠券。
现在有b元钱,问最多能购买多少商品。

考虑用树上动态规划来解决问题。
设f[u][k]表示以u为根的子树买k个物品的最小花费,且物品u使用了优惠卷;
设g[u][k]表示以u为根的子树买k个物品的最小花费,且物品u不使用优惠卷;
假设v是u的一个儿子结点,那么状态转移方程为:
f’[u][i + j] = min(f’[u][i + j], f[v][j] + f[u][i]);
f’[u][i + j] = min(f’[u][i + j], g[v][j] + f[u][i]);
g’[u][i + j] = min(g’[u][i + j], g[v][j] + g[u][i]);
理论复杂度O(n3)
树上动态规划的独特优化可以做到O(n2),详见参考代码,着重注意循环的界限以及size域的变化。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 5010
#define inf 0x3f3f3f3f

int cnt=-1,head[N];
struct Edge{int to,nxt;}e[N<<1];
int n,b,ans;
int c[N],d[N],w[N];
int dp[N][N][2],size[N];

void adde(int u,int v)
{
    e[++cnt].to=v;
    e[cnt].nxt=head[u];
    head[u]=cnt;
}
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;i=e[i].nxt)
    {
        int v=e[i].to;dfs(v);
        for(int j=size[u];j>=0;j--)
        for(int k=0;k<=size[v];k++)
        {
            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()
{
    freopen("shopping.in","r",stdin);
    freopen("shopping.out","w",stdout);
    memset(head,-1,sizeof(head));
    memset(dp,inf,sizeof(dp));
    scanf("%d%d",&n,&b);
    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);
        adde(u,i);  
    }
    dfs(1);ans=n;
    while(dp[1][ans][0]>b&&dp[1][ans][1]>b) ans--;
    printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值