zoukankan      html  css  js  c++  java
  • 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 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.

    树上背包,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])

    注意初始化和边界调节:

    F[x][0][0]是要赋为0的,因为当前节点不打折时是可以不选的,而F[x][0][1]不能.

    虽然是n^3但能过,就没必要打多叉树转二叉树

    转载自YZH神犇%%%%OTTTTTZ

    http://www.cnblogs.com/Yuzao/p/7074373.html

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long lol;
     7 struct Node
     8 {
     9   int next,to;
    10 }edge[200001];
    11 int head[5001],num,n,size[5001];
    12 lol b,w[5001],d[5001],v[5001],f[5001][5001][2];
    13 void add(int u,int v)
    14 {
    15   num++;
    16   edge[num].next=head[u];
    17   head[u]=num;
    18   edge[num].to=v;
    19 }
    20 void dfs(int x)
    21 {int i,j,k;
    22   f[x][0][0]=0;
    23   f[x][1][1]=v[x];
    24   f[x][1][0]=w[x];
    25   size[x]=1;
    26   for (i=head[x];i;i=edge[i].next)
    27     {
    28       int v=edge[i].to;
    29       dfs(v);
    30       for (j=size[x];j>=0;j--)
    31     {
    32       for (k=0;k<=size[v];k++)
    33         {
    34           f[x][j+k][0]=min(f[x][j+k][0],f[x][j][0]+f[v][k][0]);
    35           f[x][j+k][1]=min(f[x][j+k][1],f[v][k][0]+f[x][j][1]);
    36           f[x][j+k][1]=min(f[x][j+k][1],f[v][k][1]+f[x][j][1]);
    37         }
    38     }
    39       size[x]+=size[v];
    40     }
    41 }
    42 int main()
    43 {int i,fa;
    44   cin>>n>>b;
    45   scanf("%I64d%I64d",&w[1],&d[1]);
    46   v[1]=w[1]-d[1];
    47   for (i=2;i<=n;i++)
    48     {
    49       scanf("%I64d%I64d%d",&w[i],&d[i],&fa);
    50       v[i]=w[i]-d[i];
    51       add(fa,i);
    52     }
    53   memset(f,127/2,sizeof(f));
    54   dfs(1);
    55   for (i=n;i>=0;i--)
    56     if (f[1][i][1]<=b||f[1][i][0]<=b)
    57       {
    58     cout<<i<<endl;
    59     return 0;
    60       }
    61 }
  • 相关阅读:
    POJ 1681 Painter's Problem(高斯消元法)
    HDU 3530 Subsequence(单调队列)
    HDU 4302 Holedox Eating(优先队列或者线段树)
    POJ 2947 Widget Factory(高斯消元法,解模线性方程组)
    HDU 3635 Dragon Balls(并查集)
    HDU 4301 Divide Chocolate(找规律,DP)
    POJ 1753 Flip Game(高斯消元)
    POJ 3185 The Water Bowls(高斯消元)
    克琳:http://liyu.eu5.org
    WinDbg使用
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/7689210.html
Copyright © 2011-2022 走看看