好难啊,我弱爆了。

题解看陈启峰的论文。。。

/**
* Problem:POJ2152
* Author:Shun Yao
* Time:2013.9.2
* Result:Accepted
* Memo:TreeDP
*/ #include <cstring>
#include <cstdio>
#include <climits> #define MAXN 1010
#define inf LONG_MAX long n, w[MAXN], d[MAXN], f[MAXN][MAXN], ff[MAXN], dist[MAXN]; long gtot;
class Edge {
public:
long v, w;
Edge *next;
Edge() {}
~Edge() {}
Edge(long V, long W, Edge *ne) : v(V), w(W), next(ne) {}
} *g[MAXN], gg[MAXN << 1]; void add(long x, long y, long w) {
g[x] = &(gg[gtot++] = Edge(y, w, g[x]));
g[y] = &(gg[gtot++] = Edge(x, w, g[y]));
} long min(long x, long y) {
return x < y ? x : y;
} void dis(long x) {
Edge *e;
for (e = g[x]; e; e = e->next)
if (dist[e->v] == -1) {
dist[e->v] = dist[x] + e->w;
dis(e->v);
}
} void dfs(long x, long pa) {
long i;
Edge *e;
for (e = g[x]; e; e = e->next)
if (e->v != pa)
dfs(e->v, x);
memset(dist, -1, sizeof dist);
dist[x] = 0;
ff[x] = inf;
dis(x);
for (i = 1; i <= n; ++i)
if (dist[i] > d[x])
f[x][i] = inf;
else {
f[x][i] = w[i];
for (e = g[x]; e; e = e->next)
if (e->v != pa)
f[x][i] += min(ff[e->v], f[e->v][i] - w[i]);
ff[x] = min(ff[x], f[x][i]);
}
} int main() {
static long T, i, j, u, v, l; #ifndef ONLINE_JUDGE
freopen("poj2152.in", "r", stdin);
freopen("poj2152.out", "w", stdout);
#endif scanf("%ld", &T);
while (T--) {
scanf("%ld", &n);
for (i = 1; i <= n; ++i)
scanf("%ld", w + i);
for (i = 1; i <= n; ++i)
scanf("%ld", d + i);
gtot = 0;
for (i = 1; i <= n; ++i)
g[i] = 0;
for (i = 1; i < n; ++i) {
scanf("%ld%ld%ld", &u, &v, &l);
add(u, v, l);
}
dfs(1, 0);
printf("%ld\n", ff[1]);
} fclose(stdin);
fclose(stdout);
return 0;
}

poj2152 Fire的更多相关文章

  1. POJ2152 Fire 【树形dp】

    题目链接 POJ2152 题解 经典老题,还真暴力 \(n \le 1000\),所以可以\(O(n^2)\)做 所以可以枚举每个点依附于哪一个点 设\(f[u]\)表示以\(u\)为根的子树的最小代 ...

  2. poj2152 Fire(树形DP)

    题目链接:https://vjudge.net/problem/POJ-2152 题意:给定一颗大小为n的树,在每个结点建消防站花费为w[i],如果某结点没有消防站,只要在它距离<=d[i]的结 ...

  3. POJ-2152 Fire (树形DP)

    题目大意:在一棵树中选出一些点,选每个点的代价为w(i),并且对于点 i ,在距离它lim(i)之内必须选一个点,使它作为 i 的依赖点.求最小代价. 题目分析:定义状态dp(u,k)表示使u为根节点 ...

  4. POJ2152 Fire (树形DP)

    题意:n个城市n-1条边 组成一棵树 在每个城市修建消防站会有一个花费costi 每个城市能防火当且仅当地图上距离他最近的消防站距离小于di   问如何修建消防站 使地图上所有的城市都有预防火灾的能力 ...

  5. [SOJ #498]隔膜(2019-10-30考试)/[POJ2152]Fire

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

  6. 【POJ2152】Fire

    题目大意:给定一棵 N 个节点的无根树,点有点权,边有边权,现需要选出一个点集,满足树上任意一个点到该点集的距离不超过该点的给定值,求选出点集点权的最小值是多少. 题解:可以发现,对于以 i 为根的子 ...

  7. POJ 2152 fire / SCU 2977 fire(树型动态规划)

    POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...

  8. [poj2152]fire_树形dp

    fire poj-2152 题目大意:给出一颗树,给出两个相邻节点的距离,以及每个节点的接受范围,还有当前节点的代价.我们想要求出覆盖整个图的最小代价. 注释:一个点被覆盖,当且仅当该点有防火站或者这 ...

  9. 关于SequeezeNet中的Fire Module

    在论文<SQUEEZENET: ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE>中,作者 ...

随机推荐

  1. uva 147

    一个简单的dp   面值是5的倍数  将面值都除5   因为输出问题wa .... #include <iostream> #include <cstring> #includ ...

  2. use sql trigger call java function

    Use UDF sys_exec to do this. You can use this link to use sys_exec function. It says, sys_exec sys_e ...

  3. CentOS 报no acceptable C compiler found in $PATH的解决办法

    CentOS 6.2下安装tcpreplay工具的时候,先安装libpcap-1.3.0,configure libpcap时出错. #./configure 提示没有GCC编译器环境) config ...

  4. ZOJ 1008 Gnome Tetravex(DFS)

    题目链接 题意 : 将n*n个正方形进行排列,需要判断相邻的正方形的相邻三角形上边的数字是不是都相等. 思路 : 只知道是个深搜,一开始不知道怎么搜,后来看了题解才明白,就是说不是自己去搜,而是将给定 ...

  5. hdu 2509 Be the Winner 博弈论

    博弈论水题!!! 代码如下: #include<stdio.h> #include<iostream> using namespace std; int main(){ int ...

  6. pointcut 表达式的含义

    execution(* com.spring.dao.*.add*(..)) 第一个*表示任意返回值 第二个*表示com.spring.dao包中所有类 第三个*表示以add开头的所有方法 (..)表 ...

  7. OpenStack 部署运维实战

    http://www.ibm.com/developerworks/cn/cloud/library/1408_zhangxl_openstack/#icomments 本文为您介绍了网易公司基于 O ...

  8. QTreeView只显示指定驱动器及其目录,隐藏所有兄弟节点

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setup ...

  9. CityEngine 2013部署安装

    安装环境: windows8.1 专业版 已安装arcgis10.2 2的授权方式也同样分为:单机许可和浮动 许可.单机许可是将许可部署在本机直接使用:浮动许可是部署到服务器上通过IP地址连接,可借出 ...

  10. dup和dup2函数以及管道的实现

    疑问:管道应该不是这样实现的,因为这要求修改程序的代码 dup和dup2也是两个非常有用的调用,它们的作用都是用来复制一个文件的描述符.它们经常用来重定向进程的stdin.stdout和stderr. ...