poj2152 Fire
好难啊,我弱爆了。
题解看陈启峰的论文。。。
/**
* 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的更多相关文章
- POJ2152 Fire 【树形dp】
题目链接 POJ2152 题解 经典老题,还真暴力 \(n \le 1000\),所以可以\(O(n^2)\)做 所以可以枚举每个点依附于哪一个点 设\(f[u]\)表示以\(u\)为根的子树的最小代 ...
- poj2152 Fire(树形DP)
题目链接:https://vjudge.net/problem/POJ-2152 题意:给定一颗大小为n的树,在每个结点建消防站花费为w[i],如果某结点没有消防站,只要在它距离<=d[i]的结 ...
- POJ-2152 Fire (树形DP)
题目大意:在一棵树中选出一些点,选每个点的代价为w(i),并且对于点 i ,在距离它lim(i)之内必须选一个点,使它作为 i 的依赖点.求最小代价. 题目分析:定义状态dp(u,k)表示使u为根节点 ...
- POJ2152 Fire (树形DP)
题意:n个城市n-1条边 组成一棵树 在每个城市修建消防站会有一个花费costi 每个城市能防火当且仅当地图上距离他最近的消防站距离小于di 问如何修建消防站 使地图上所有的城市都有预防火灾的能力 ...
- [SOJ #498]隔膜(2019-10-30考试)/[POJ2152]Fire
题目大意:有一棵$n$个点的带边权树,第$i$个点有两个值$w_i,d_i$,表示在这个点做标记的代价为$w_i$,且这个点距离$d_i$以内至少要有一个点被标记,为最小代价.$n\leqslant6 ...
- 【POJ2152】Fire
题目大意:给定一棵 N 个节点的无根树,点有点权,边有边权,现需要选出一个点集,满足树上任意一个点到该点集的距离不超过该点的给定值,求选出点集点权的最小值是多少. 题解:可以发现,对于以 i 为根的子 ...
- POJ 2152 fire / SCU 2977 fire(树型动态规划)
POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...
- [poj2152]fire_树形dp
fire poj-2152 题目大意:给出一颗树,给出两个相邻节点的距离,以及每个节点的接受范围,还有当前节点的代价.我们想要求出覆盖整个图的最小代价. 注释:一个点被覆盖,当且仅当该点有防火站或者这 ...
- 关于SequeezeNet中的Fire Module
在论文<SQUEEZENET: ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE>中,作者 ...
随机推荐
- uva 10791
还算比较水的一个数学题 求因子的最小和 总是用小的数去除 注意特判 是用int不行哦........ #include <cstdio> #include <cmath> ...
- fiddler插件开发step by step 1
Fiddler 是优秀的抓包工具,有着众多的优秀插件.Fiddler 软件是由C#语言开发的,运行在.net Framework 框架之上,所以我们也可以使用vs来开发自己的Fiddler插件,下面就 ...
- VS2005 VS2008 Manifest 配置问题总结
一.问题 编译某个遗留工程后,运行程序时报错,“由于应用程序的配置不正确,应用程序无法启动.重新安装应用程序可能会解决这个问题.” 查看生成的Manifest文件如下: <?xml versio ...
- hdu 4291 A Short problem
数学题,找循环节!! 首先g(g(g(n)))=g(x) mod 1e9+7 则可知x有循环节1e9+7; 之后x=g(g(n)),则可算出g(n)的循环节,在算出n的循环节就可以了!! 代码如下: ...
- java jdbc dbcp连接SQL Server
使用到的jar: commons-collections-3.1.jar commons-dbcp-1.4.jar commons-pool-1.5.6.jar sqljdbc4.jar dbcp配置 ...
- 6大排序算法,c#实现
using System; using System.Text; using System.Collections.Generic; namespace ArithmeticPractice { st ...
- [itint5]判断是否为二叉搜索树
http://www.itint5.com/oj/#25 这题在leetcode上是用中序遍历来做的,但是这里由于有相等的情况,即左子树小于等于根,这样中序遍历无法完全判定.可以用递归来做,用递归给出 ...
- Qt读写二进制文件
http://blog.csdn.net/mjlsuccess/article/details/22194653 http://www.cnblogs.com/weiweiqiao99/archive ...
- H264/AVC视频解码时AVC1和H264的区别
AVC1与H264的区别 http://blog.csdn.net/qiuchangyong/article/details/6660253 H.264 Video Types The followi ...
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...