【POJ 2152】 Fire
【题目链接】
【算法】
同样是树形DP,但是比较难,笔者做这题看了题解
令f[i][j]表示在以i为根的子树中
1.在以i为根的子树中建一些消防站
2.在节点j必须建一个消防站
3.以i为根的子树中,每个节点在满足距离不超过D的前提下,选一个子树内的节点或节点j作为“负责站”
4.节点i的负责站必须是节点j
的最小代价
考虑转移,为了转移方便,我们用一个辅助状态best[i]表示以i为根的子树中,每个节点在满足距离不超过D的前提下,
选一个子树内的节点作为“负责站”的最小代价,显然 : best[i] = min{f[i][j]}(j在以i为根的子树中)
当dis(i,j) > Di时,f[i][j] = +oo(正无穷,表示不存在这种状态
当dis(i,j) <= Di时,它的每个子节点k有两种选择 :
1.选择子树内的节点为“负责站”,代价为best[k]
2.选择j为它的“负责站”,代价为f[k][j]
因此f[i][j] = w[j] + sigma(min{best[k],f[k][j]}) (k为i的孩子)
最后,best[1]就是答案
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 1010
const int INF = 2e9; int i,T,n,u,v,l;
vector< pair<int,int> > e[MAXN];
int dis[MAXN],best[MAXN],w[MAXN],d[MAXN],f[MAXN][MAXN]; inline void getdist(int x)
{
int i,y;
for (i = ; i < e[x].size(); i++)
{
y = e[x][i].first;
if (dis[y] == -)
{
dis[y] = dis[x] + e[x][i].second;
getdist(y);
}
}
}
inline void dfs(int x,int fa)
{
int i,j,y;
for (i = ; i < e[x].size(); i++)
{
y = e[x][i].first;
if (fa != y) dfs(y,x);
}
for (i = ; i <= n; i++) dis[i] = -;
dis[x] = ;
getdist(x);
best[x] = INF;
for (i = ; i <= n; i++) f[x][i] = INF;
for (i = ; i <= n; i++)
{
if (dis[i] <= d[x])
{
f[x][i] = w[i];
for (j = ; j < e[x].size(); j++)
{
y = e[x][j].first;
if (fa != y) f[x][i] += min(best[y],f[y][i]-w[i]);
}
best[x] = min(best[x],f[x][i]);
}
}
} int main()
{ scanf("%d",&T); while (T--)
{
scanf("%d",&n);
for (i = ; i <= n; i++) e[i].clear();
for (i = ; i <= n; i++) scanf("%d",&w[i]);
for (i = ; i <= n; i++) scanf("%d",&d[i]);
for (i = ; i < n; i++)
{
scanf("%d%d%d",&u,&v,&l);
e[u].push_back(make_pair(v,l));
e[v].push_back(make_pair(u,l));
}
dfs(,);
printf("%d\n",best[]);
} return ; }
【POJ 2152】 Fire的更多相关文章
- 【POJ 2152】 Fire (树形DP)
Fire Description Country Z has N cities, which are numbered from 1 to N. Cities are connected by h ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- bzoj5108 [CodePlus2017]可做题 位运算dp+离散
[CodePlus2017]可做题 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 87 Solved: 63[Submit][Status][Dis ...
- 12.3——类作用域,构造函数,友元,static类成员
类作用域: (1)成员函数在类外定义时,因为函数体还有形参列表都出现在成员名之后,都是在类作用域内定义,所以不用加域作用符 来引用其他的成员. (2)函数的返回值不一定需要在类的作用域中,但是若是返回 ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- 解决Flex4在Module里调用PopUpManager报错问题
项目大了 就需要用到Module, 发现在一个Module里边, 和普通应用里一样popup一个组件的时候, 总是会报错. 这个在Flex3的时候也会出现, 会报样式错误 所以就想到了addPopUp ...
- Swift 了解
本篇仅于个人小记,记录个人不熟悉的知识点儿.如若要了解更全,请前往如下网址:http://www.runoob.com/swift/swift-arrays.html 1.Swift 标记 分号:Sw ...
- POJ 3461 字符串出现次数 && HDU1711 字符串第一次出现的位置 模板题
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48387 Accepted: 19261 Descri ...
- Windows 上通过本地搭建 Jekyll环境
一 准备Ruby环境 1 我们首先须要安装Ruby.从站点下载Ruby 上下载Ruby最新版和对应的DevKit. 我下载的是Ruby 2.1.4 (x64)和DevKit-mingw64-6 .注意 ...
- FPGA第一篇:SRAM工作原理
一.SRAM概述 SRAM主要用于二级快速缓存(Level2 C ache). 它利用晶体管来存储数据.与DRAM相比,SRAM的速度快,但在同样面积中SRAM的容量要比其它类型的内存小. 大部分FP ...
- 杭电1232畅通project
畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 关于 equals()与hashcode()方法
类重写了equals()方法也必须重写hashcode()方法,否则会导致该类无法与基于散列值的集合(HashMap.HashSet.HashTable)一起正常使用. hashcode()方法遵循的 ...