Fire (poj 2152 树形dp)

给定一棵n个结点的树(1<n<=1000)。现在要选择某些点,使得整棵树都被覆盖到。当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同时花费为v[i]。问最小花费。

以前做过一道类似的题(水库),这道题也差不多。首先来考虑,用\(best[i]\)表示以i为根的子树的最小花费。这样做有什么问题呢?它无法很好的处理消防站重复建的问题。

所以换一种做法。\(best[i]\)依然表示原来的含义,新建一个数组\(f[i][j]\),表示当i这个结点,依赖j的消防站时的最小花费。转移方程就是:\(f[i][j]=v[i]+\sum min(f[son_k][j]-v[j], best[son_k])\)。注意当\(dis(i, j)>d[i]\)时,\(f[i][j]=\infty\)。它的思想就是如果i依赖j,就直接让子树中依赖j的点都减去依赖,从而消除影响。

(傻逼了,用rmq求树上两点距离)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn=1005, maxm=1005, logn=12, INF=1e9; struct Graph{
struct Edge{
int to, next, v; Graph *bel;
inline int operator*(){ return to; }
Edge& operator ++(){
return *this=bel->edge[next]; }
}edge[maxm*2];
void reset(){
memset(fir, 0, sizeof(fir)); cntedge=0; }
void addedge(int x, int y, int v){
Edge& e=edge[++cntedge];
e.to=y; e.next=fir[x]; e.v=v;
e.bel=this; fir[x]=cntedge; }
Edge& getlink(int x){ return edge[fir[x]]; }
int cntedge, fir[maxn];
}g; //初始化及树形dp
int T, n, mi[logn], w[maxn], d[maxn];
int best[maxn], dp[maxn][maxn];
//求两点距离(rmq lca)
int id[maxn*2], fir[maxn], dep[maxn], time;
int st[maxn*2][logn]; void predfs(int now, int par){
Graph::Edge e=g.getlink(now);
id[++time]=now; fir[now]=time;
for (; *e; ++e){
if (*e==par) continue;
dep[*e]=dep[now]+e.v; predfs(*e, now);
id[++time]=now;
}
} int mindep(int x, int y){
return (dep[x]<dep[y])?x:y; } void init_st(){
for (int i=1; i<=n*2; ++i) st[i][0]=id[i];
for (int i=1; i<logn; ++i)
for (int j=1; j<=n*2; ++j) if (j+mi[i]-1<=n*2)
st[j][i]=mindep(st[j][i-1], st[j+mi[i-1]][i-1]);
} int log2(float x){
return ((unsigned&)x>>23&255)-127;
} int getlca(int x, int y){
if (fir[x]>fir[y]) swap(x, y);
int fx=fir[x], fy=fir[y];
int logxy=log2(fy-fx+1);
return mindep(st[fx][logxy],
st[fy-mi[logxy]+1][logxy]);
} int dis(int x, int y){
int lca=getlca(x, y);
return dep[x]+dep[y]-2*dep[lca];
} void dfs(int now, int par){
Graph::Edge e=g.getlink(now);
for (; *e; ++e) if (*e!=par) dfs(*e, now);
e=g.getlink(now); best[now]=INF;
for (int j=1; j<=n; ++j)
dp[now][j]=(dis(now, j)<=d[now]?w[j]:INF);
for (; *e; ++e) if (*e!=par)
for (int j=1; j<=n; ++j) if (dis(now, j)<=d[now])
dp[now][j]+=min(dp[*e][j]-w[j], best[*e]);
for (int j=1; j<=n; ++j)
best[now]=min(best[now], dp[now][j]);
} int main(){
scanf("%d", &T); int x, y, l;
mi[0]=1; for (int i=1; i<logn; ++i) mi[i]=mi[i-1]*2;
while (T--){
g.reset(); scanf("%d", &n);
for (int i=1; i<=n; ++i) scanf("%d", &w[i]);
for (int i=1; i<=n; ++i) scanf("%d", &d[i]);
for (int i=1; i<n; ++i){
scanf("%d%d%d", &x, &y, &l);
g.addedge(x, y, l); g.addedge(y, x, l);
}
time=0; predfs(1, 0); dep[1]=0;
init_st(); dfs(1, 0);
printf("%d\n", best[1]);
}
return 0;
}

Fire (poj 2152 树形dp)的更多相关文章

  1. poj 2152 树形DP

    思路:这个没思路,看了陈启峰的论文写得. #include<map> #include<set> #include<cmath> #include<queue ...

  2. poj 1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...

  3. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...

  4. poj 3140(树形dp)

    题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...

  5. Strategic game(POJ 1463 树形DP)

    Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 De ...

  6. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  7. poj 3345 树形DP 附属关系+输入输出(好题)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/17665 参考资料:http://blog.csdn.net/woshi250hua/article/detai ...

  8. POJ 1155 树形DP

    题意:电视台发送信号给很多用户,每个用户有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号. 转自:http://www.cnblogs.com/andre050 ...

  9. POJ 3342 树形DP+Hash

    这是很久很久以前做的一道题,可惜当时WA了一页以后放弃了. 今天我又重新捡了起来.(哈哈1A了) 题意: 没有上司的舞会+判重 思路: hash一下+树形DP 题目中给的人名hash到数字,再进行运算 ...

随机推荐

  1. Pentaho BIServer Community Edtion 6.1 使用教程 第四篇 安装和使用Saiku 插件 进行 OLAP

    OLAP(On-Line Analytical Processing,联机分析处理)是一个使分析师.管理者和执行者从原始数据中用来快速.一致.交互访问的一种软件技术,从而真实的反映企业的数据情况.OL ...

  2. SlopeOne推荐算法

           Slope One 算法 是一种基于评分的预测算法, 本质上也是一种基于项目的算法.与一般的基于项目的算法不同, 该算法不计算项目之间的相似度, 而是用一种简单的线性回归模型进行预测(可 ...

  3. python获取当前的时间

    打印出当前的年月日时分秒 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) 2018-09-05 09:39: ...

  4. Linux环境下使用dosemu写汇编

    本章学习内容是汇编语言,现在直接写汇编的机会不多了,但一定要能读懂,信息安全的核心思维方式“逆向”在这有很好很直接的体现,反汇编就是直接的逆向工程. 所以我在前几天的学习中在Ubuntu环境下安装了可 ...

  5. ZOJ - 3430 Detect the Virus —— AC自动机、解码

    题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds      Memory Limit: 6 ...

  6. BZOJ 4582 [Usaco2016 Open]Diamond Collector:贪心【相差不超过k】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4582 题意: 给你n个数. 让你将其中的一些数放入两个不同的集合中,并保证同一集合内两两元 ...

  7. linux应用之开机自启动方法总结

    1.把启动程序的命令添加到/etc/rc.d/rc.local文件中 CentOS系统下管理开机自启动的配置文件是/etc/rc.d/rc.local,所以只需编辑这个文件,在里面加入相应的启动命令即 ...

  8. hdu 1042 N!(大数)

    题意:求n!(0 ≤ N ≤ 10000) 思路:大数,用数组存储 1.首先要考虑数据N!的位数,因为最大是10000!,可以计算一下大概是5+9000*4+900*3+90*2+10*1=38865 ...

  9. checkbox怎么判断是否选中

    下面这种可以使用 if($("#checkbox1").is(':checked')) { alert("1"); } else { alert("0 ...

  10. 【thrift】vc中使用thrift中文字符串乱码问题解决

    问题描述: VC中使用Apache thrift时,如果字符串中包含中文,会出现乱码问题,这个问题的原因是由于thrift为了达到跨语言交互而使用了UTF-8格式发送字符串,这点对java或者C#不会 ...