题目链接

CF815C Karen and Supermarket

题解

只要在最大化数量的前提下,最小化花费就好了

这个数量枚举ok,

dp[i][j][1/0]表示节点i的子树中买了j件商品 i 优惠了 / 没优惠

复杂度是n^2的

因为每次是新儿子节点的siz * 之前儿子几点的siz,

就相当于树上的节点两两匹配,这个匹配只会在lca处计算一次

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') { if(c == '-')f = -1; c = getchar(); }
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
const int maxn = 5007;
int n; LL b;int c[maxn], d[maxn];
struct node {
int v,nxt;
} edge[maxn];
int head[maxn],num = 0 ;
inline void add_edge(int u,int v) {
edge[++ num].v = v; edge[num].nxt = head[u];head[u] = num;
}
LL dp[maxn][maxn][2];
int siz[maxn];
void dfs(int x) {
siz[x] = 1;
dp[x][0][0] = 0;
dp[x][1][0] = c[x] ;
dp[x][1][1] = c[x] - d[x];
//for(int i = head[x];i;i = edge[i].nxt) dfs(edge[i].v), siz[x] += siz[edge[i].v];
for(int i = head[x];i;i = edge[i].nxt) {
int v = edge[i].v;
dfs(v);
for(int j = siz[x];j >= 0;-- j) {
for(int k = 0;k <= siz[v];++ k) {
dp[x][j + k][0] = std::min(dp[x][j + k][0],dp[x][j][0] + dp[v][k][0]);
dp[x][j + k][1] = std::min(dp[x][j + k][1],dp[x][j][1] + std::min(dp[v][k][1],dp[v][k][0]));
}
} siz[x] += siz[v];
}
}
int main() {
memset(dp,0x3f,sizeof dp);
n = read(), b = read();
c[1] = read(); d[1] = read();
for(int pre, i = 2;i <= n;++ i) {
c[i] = read(),d[i] = read(); pre = read();
add_edge(pre,i);
}
dfs(1);
int ans = 0;
for(int i = 1;i <= n;++ i)
if(std::min(dp[1][i][0],dp[1][i][1]) <= b)ans = i;
printf("%d\n",ans);
return 0;
}

CF815C Karen and Supermarket的更多相关文章

  1. CF815C Karen and Supermarket [树形DP]

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

  2. Codeforces 815C Karen and Supermarket 树形dp

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

  3. E. Karen and Supermarket

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

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

  5. 「CF815C」Karen and Supermarket

    传送门 Luogu 解题思路 树形背包. 设 \(f[i][j][0/1]\) 表示在以 \(i\) 为根的子树中选 \(j\) 件商品的最少花费. 边界条件: \(f[i][j][0] = \min ...

  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. python---django中orm的使用(3)admin配置与使用

    新建项目,并开启 python manage.py runserver 访问admin页面 http://127.0.0.1:8080/admin 补充:若是发现admin页面样式丢失:可能是因为在s ...

  2. Spark记录-Scala异常与处理

    Scala try-catch语句 Scala提供try和catch块来处理异常.try块用于包含可疑代码.catch块用于处理try块中发生的异常.可以根据需要在程序中有任意数量的try...cat ...

  3. Confluence wiki——CentOS6.8搭建详解

    参考资料:http://www.cnblogs.com/jackyyou/p/5534231.html http://www.ilanni.com/?p=11989 公司需要搭建WIKI方便员工将一些 ...

  4. [转载]Understanding the Bootstrap 3 Grid System

    https://scotch.io/tutorials/understanding-the-bootstrap-3-grid-system With the 3rd version of the gr ...

  5. Servlet笔记9--转发与重定向

    关于Web中资源跳转的问题: 转发和重定向基本代码: package com.bjpowernode.javaweb; import java.io.IOException; import javax ...

  6. com.alibaba.fastjson.JSONException: autoType is not support.

    解决办法:https://github.com/alibaba/fastjson/wiki/enable_autotype 文摘如下: 一.添加autotype白名单 添加白名单有三种方式,三选一,如 ...

  7. jquery-easyui:如何设置组件属性

    在这里以面板为例: $().ready(function() { $('#menu').tree({ url : '/menu', onClick : function(node) { $('#cen ...

  8. Ubuntu 12.04 安装Tomcat7

    1.下载Tomcat7 打开Tomcat官网 http://tomcat.apache.org,在左边的导航栏的“Download"中找到Tomcat7.0目录,点击后进入Tomcat7的页 ...

  9. javaweb笔记五

    JSP:java server page服务器脚本语言.(脚本===插件),是一种在html代码中,嵌入java代码的方式.解决servlet产生动态页面缺陷而产生的一门技术.js:客户端脚本语言js ...

  10. MVC -18.缓存(2)

    一.MVC缓存简介 缓存是将信息(数据或页面)放在内存中以避免频繁的数据库存储或执行整个页面的生命周期,直到缓存的信息过期或依赖变更才再次从数据库中读取数据或重新执行页面的生命周期.在系统优化过程中, ...