【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][ ...
随机推荐
- PTA 04-树5 Root of AVL Tree (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree (25分) An AVL tree ...
- [luoguP2948] [USACO09OPEN]滑雪课Ski Lessons(DP)
传送门 f[i][j]表示i时刻能力值为j的最大滑雪数 显然f[0][1]=0,开始搜索 三种转移: ①美美的喝上一杯**:f[i+1][j]=max(f[i+1][j],f[i][j]) ②滑雪,f ...
- apache cgi 模块安装
apache安装,请参照:http://httpd.apache.org/docs/2.4/ 安装系统: Fedora release 21 (Twenty One) (x64) 版本:Server ...
- python之-- socket 基础篇
socket 网络模块 注意事项:在python3中,所有数据的传输必须用bytes类型(bytes只支持ascii码)所以在发送数据的时候要么在发送的字符串前面加 'b',要么使用encode('u ...
- python学习之 - XML
xml模块定义:实现不同语言或程序之间进行数据交换的协议.格式如下:通过<>节点来区别数据结构如:<load-on-startup(这个是标签) test="value&q ...
- Python基础之 一 文件操作
文件操作 流程: 1:打开文件,得到文件句柄并赋值给一个变量 2:通过句柄对文件进行操作 3:关闭文件 模式解释 r(读) , w(写) ,a(附加)r+(读写的读), w+(读写的写),a+(读附加 ...
- HDU 1074 Doing Homework【状态压缩DP】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...
- 大数c++模板 超级好用
只用输入用cin 输出 cout 每个数学符号都可以用 超级强大 #include <iostream> #include <queue> #include <c ...
- <项目><day12>通讯录(视频)
1 需求分析(需求分析师) 功能分析: 1)添加联系人 2)修改联系人 3)删除联系人 4)查询所有联系人 2 需求设计(系统分析师/架构师/资深开发人员) 2.1设计实体(抽象实体) 联系人实体: ...
- Spring AOP Capability and Goal
AOP Capability: 1.Spring声明式事务管理配置. 2.Controller层的参数校验. 3.使用Spring AOP实现MySQL数据库读写分离案例分析 4.在执行方法前,判断是 ...