题目大意:有一棵$n$个点的带边权树,第$i$个点有两个值$w_i,d_i$,表示在这个点做标记的代价为$w_i$,且这个点距离$d_i$以内至少要有一个点被标记,为最小代价。$n\leqslant6000$

题解:记$f[i][j]$表示以$i$为根的子树全部满足条件,且第$i$个点是由于$j$被标记导致的,$g[i]$表示以$i$为根的子树全部满足条件的代价。$f[i][j]=w_i+\min\limits_{v\in son[u]}\{f[v][j]-w_i,g[v]\}$

卡点:

C++ Code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
const int maxn = 6e3 + 10, inf = 0x3f3f3f3f; int n, w[maxn], d[maxn];
int head[maxn], cnt;
struct Edge {
int to, nxt, w;
} e[maxn << 1];
void addedge(int a, int b, int c) {
e[++cnt] = (Edge) { b, head[a], c }; head[a] = cnt;
e[++cnt] = (Edge) { a, head[b], c }; head[b] = cnt;
} int dis[maxn][maxn], f[maxn][maxn], g[maxn];
void dfs0(int *dis, int u, int fa = 0) {
for (int i = head[u], v; i; i = e[i].nxt) {
v = e[i].to;
if (v != fa) {
dis[v] = dis[u] + e[i].w;
dfs0(dis, v, u);
}
}
} void dfs(int u, int fa = 0) {
for (int i = head[u]; i; i = e[i].nxt)
if (e[i].to != fa) dfs(e[i].to, u);
for (int i = 1; i <= n; ++i) if (dis[u][i] <= d[u]) {
f[u][i] = w[i];
for (int j = head[u], v; j; j = e[j].nxt) {
v = e[j].to;
if (v != fa) f[u][i] += std::min(f[v][i] - w[i], g[v]);
}
g[u] = std::min(g[u], f[u][i]);
}
} int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> n;
for (int i = 1; i <= n; ++i) std::cin >> w[i];
for (int i = 1; i <= n; ++i) std::cin >> d[i];
for (int i = 1, a, b, c; i < n; ++i) {
std::cin >> a >> b >> c;
addedge(a, b, c);
}
for (int i = 1; i <= n; ++i) dfs0(dis[i], i);
memset(f, 0x3f, sizeof f), memset(g, 0x3f, sizeof g);
dfs(1); std::cout << g[1] << '\n';
return 0;
}

  

[SOJ #498]隔膜(2019-10-30考试)/[POJ2152]Fire的更多相关文章

  1. 2019.3.28&2019.3.30考试

    2019.3.28 : 肥肠爆芡,因为这场考试的题太屑了,所以我咕咕了 Upd on 2019.3.30 压进来一篇(因为都没啥意义) 2019.3.30 : 全机房读错题+没有大样例=T2全体爆炸 ...

  2. 【2019.10.30】SDN上机第1次作业

    用字符命令搭建如下拓扑,要求写出命令 题目一: 字符命令如下: 题目二: 字符命令如下: 利用可视化工具搭建如下拓扑 要求支持OpenFlow 1.0 1.1 1.2 1.3,设置h1(10.0.0. ...

  3. 2019.10.30 csp-s模拟测试94 反思总结

    头一次做图巨的模拟题OWO 自从上一次听图巨讲课然后骗了小礼物以后一直对图巨印象挺好的233 T1: 对于XY取对数=Y*log(x) 对于Y!取对数=log(1*2*3*...*Y)=log1+lo ...

  4. Alpha冲刺(7/10)——2019.4.30

    所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(7/10)--2019.4.30 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪 ...

  5. gnuWin32-mini-2016.10.30

    2016-10-28 04:48 1,017,856 awk.exe ver 4.1.4 2016-10-29 00:26 77,312 bc.exe ver 1.06 2016-10-30 01:4 ...

  6. 10.30 正睿停课训练 Day12

    目录 2018.10.30 正睿停课训练 Day12 A 强军战歌(DP 树状数组 容斥) B 当那一天来临(思路) C 假如战争今天爆发(贪心) 考试代码 B C 2018.10.30 正睿停课训练 ...

  7. 【LOJ】#3030. 「JOISC 2019 Day1」考试

    LOJ#3030. 「JOISC 2019 Day1」考试 看起来求一个奇怪图形(两条和坐标轴平行的线被切掉了一个角)内包括的点个数 too naive! 首先熟练的转化求不被这个图形包含的个数 -- ...

  8. 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox

    [源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...

  9. 2016.10.30 NOIP模拟赛 day2 PM 整理

    满分:300分 直接全部爆零,真的是很坑啊! 10.30的题目+数据:链接:http://pan.baidu.com/s/1jHXLace 密码:i784 T1: 题目中的难点就是每次折叠的点可能应经 ...

随机推荐

  1. template cannot be keyed. Place the key on real elements instead.

    template cannot be keyed. Place the key on real elements instead. 一.总结 一句话总结: 原因:vue不支持在 template 元素 ...

  2. java join()基本用法与说明解释

    join()方法的作用,是等待这个线程结束: 也就是说,t.join()方法阻塞调用此方法的线程(calling thread)进入 TIMED_WAITING 状态,直到线程t完成,此线程再继续: ...

  3. mp4文件转码为m3u8

    https://bbs.csdn.net/topics/392046401 *********************************************** 转码完成,我直接播放m3u8 ...

  4. android Studio keytool' 不是内部或外部命令,也不是可运行的程序 或批处理文件

    //android Studio  keytool' 不是内部或外部命令,也不是可运行的程序 或批处理文件 遇到这个问题好久了,一直没解决今天搜集了大量的资料,有的说什么Java没配置好,不是扯犊子吗 ...

  5. zookeeper从3.4.8升级到3.4.14

    升级背景说明: 最近在做系统安全扫描时,扫出来zookeeper存在安全漏洞 Apache Zookeeper 缓冲区溢出漏洞(CVE--) 官方给出的升级建议: 地址:https://zookeep ...

  6. centos7安装Redis5.0.5

    1.下载redismkdir /home/redis/cd /home/redis/wget http://download.redis.io/releases/redis-5.0.5.tar.gzt ...

  7. 安装opencv时ippicv下载超时

    1.手动去下载: github地址为: https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20151201/ippicv 2.查 ...

  8. Ant Design Pro 子界面传值

  9. 没有可用的软件包 xxx,但是它被其它的软件包引用了

    在linux下apt安装软件,弹出这个错. 解决,更新下资源: sudo apt-get update

  10. 【452】pandas筛选出表中满足另一个表所有条件的数据

    参考:pandas筛选出表中满足另一个表所有条件的数据 参考:pandas:匹配两个dataframe 使用 pd.merge 来实现 on 表示查询的 columns,如果都有 id,那么这是很好的 ...