【题目链接】

点击打开链接

【算法】

同样是树形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的更多相关文章

  1. 【POJ 2152】 Fire (树形DP)

    Fire   Description Country Z has N cities, which are numbered from 1 to N. Cities are connected by h ...

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...

  2. 【链表】2017多校训练三 HDU 6058 Kanade's sum

    acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...

  3. [NOIP2000] 提高组 洛谷P1023 税收与补贴问题

    题目背景 每样商品的价格越低,其销量就会相应增大.现已知某种商品的成本及其在若干价位上的销量(产品不会低于成本销售),并假设相邻价位间销量的变化是线性的且在价格高于给定的最高价位后,销量以某固定数值递 ...

  4. CSS属性操作一

    CSS属性操作 一.CSS text 1.文本颜色:color 颜色属性被用来设置文字的颜色.颜色是通过CSS最经常的指定: • 十六进制值 - 如: #FF0000 • 一个RGB值 - 如: RG ...

  5. TimePickerDialog

    package com.pingyijinren.helloworld.activity; import android.app.TimePickerDialog; import android.su ...

  6. javax/servlet/ServletContext : Unsupported major.minor version 51.0

    原文:http://blog.csdn.net/mlin_123/article/details/50738532 解决:将版本从 3.1.0 改为 3.0.1 <!-- 添加servlet A ...

  7. 关闭Windows 2003/2008中IE增强的安全配置的方法

           在使用Windows Server 2003/2008操作系统时,打开IE浏览网页时,发现浏览器总提示 "是否需要将当前访问的网站添加到自己信任的站点中去",要是不信 ...

  8. php.ini中extension默许的地址到底在哪里设置的

    原文: http://www.myexception.cn/php/1436096.html ----------------------------------------------------- ...

  9. js中的自定义异常处理函数

    1. Can I suppress JavaScript error messages? 2. Can I set up my own JavaScript error handler? 3. Can ...

  10. HDU OJ Max sum 题目1003

     #include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...