POJ 2152 fire / SCU 2977 fire(树型动态规划)
POJ 2152 fire / SCU 2977 fire(树型动态规划)
Description
Country Z has N cities, which are numbered from 1 to N. Cities are connected by highways, and there is exact one path between two different cities. Recently country Z often caught fire, so the government decided to build some firehouses in some cities. Build a firehouse in city K cost W(K). W for different cities may be different. If there is not firehouse in city K, the distance between it and the nearest city which has a firehouse, can’t be more than D(K). D for different cities also may be different. To save money, the government wants you to calculate the minimum cost to build firehouses.
Input
The first line of input contains a single integer T representing the number of test cases. The following T blocks each represents a test case.
The first line of each block contains an integer N (1 < N <= 1000). The second line contains N numbers separated by one or more blanks. The I-th number means W(I) (0 < W(I) <= 10000). The third line contains N numbers separated by one or more blanks. The I-th number means D(I) (0 <= D(I) <= 10000). The following N-1 lines each contains three integers u, v, L (1 <= u, v <= N,0 < L <= 1000), which means there is a highway between city u and v of length L.
Output
For each test case output the minimum cost on a single line.
Sample Input
5
5
1 1 1 1 1
1 1 1 1 1
1 2 1
2 3 1
3 4 1
4 5 1
5
1 1 1 1 1
2 1 1 1 2
1 2 1
2 3 1
3 4 1
4 5 1
5
1 1 3 1 1
2 1 1 1 2
1 2 1
2 3 1
3 4 1
4 5 1
4
2 1 1 1
3 4 3 2
1 2 3
1 3 3
1 4 2
4
4 1 1 1
3 4 3 2
1 2 3
1 3 3
1 4 2
Sample Output
2
1
2
2
3
Http
POJ:https://vjudge.net/problem/POJ-2152
SCU:https://vjudge.net/problem/SCU-2977
Source
树型动态规划
题目大意
n个城市由n-1条带权的路相连接,现在要选择一些城市建造消防站,每个城市建造消防站都有不同的花费,并且要满足没有建消防站的城市u在D[u]范围内有建了消防站的城市(每个城市的D[u]也有可能不一样),现在求花费最小的方案。
解决思路
看到这个题目的第一眼想到的是最小支配集,但要注意题中在D[u]范围内这个条件,我们不能用简单的最小支配集算法来解决。
在开始讲述前,我们先规定几个变量及其意义。
F[u][build]:这是我们动态规划的数组,其意义是选择在build处修建消防站来覆盖u的最小花费(并保证此时u的所有子树都已经被覆盖)
Best[u]:表示覆盖u的所有方案中花费最小的一个
Dist[u][v]:u和v之间的距离
首先我们来看稍微好理解一点的Best[u],根据定义,对于所有的满足Dist[u][build]< D[u]的我们可以得到Best[u]=min(Best[u],Dist[u][build])。这是可以直接根据定义推导出来的。
那么接下来就是求F[u][build]啦。
当然首先是要递归地求出u的子节点(下文中均用v来表示)的信息。
接下来就是最重要的部分了,有点难理解,仔细阅读!
然后我们枚举图中的每一个点build,表示在build建消防站来覆盖点u。若Dist[u][build]>D[u],说明在build建立消防站不能覆盖点u,那么我们就把F[u][build]置为无穷大。若Dist[u][build]< D[u],说明可以在build建立消防站来覆盖点u,我们就依次枚举u的子节点v,让F[u][build]每次累加min(Best[v],F[v][build]-W[build]),最后再让F[u][build]加上建站在build的费用W[build]。下面来解释一下这个转移是如何进行的,又是基于什么原理。
因为在build建立消防站后可能不止覆盖到u,还有可能同时可以覆盖u的子节点v,那么此时v就有两种选择,一是被build覆盖(即上面的F[v][build]-W[build],你问我为什么要减去W[build],因为F[v][build]中是统计了建站在build时的费用的,为了防止重复计算建立消防站在build的费用,所以这里要减去);二是被其他点覆盖(即上面的Best[v])。
另外如果build无法覆盖v怎么办(即Dist[v][build]>D[v]),上面的方程好像没有考虑到这种情况啊?
不用担心,若build无法覆盖v,此时的F[v][build]是置为无穷大的,取min后不会被统计到F[u][build]中。
最后,为了节省空间,在笔者的代码中,Dist转换成了一维的,Dist[v]表示当前dfs中的点u到v的距离。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxN=1001;
const int inf=2147483647;
class Edge
{
public:
int v,w;
};
int n;
int W[maxN];
int D[maxN];
vector<Edge> E[maxN];
int Dist[maxN];
int F[maxN][maxN];
int Best[maxN];
queue<int> Q;
int read();
void dfs(int u,int father);
int main()
{
int TT;
TT=read();
for (int ti=1;ti<=TT;ti++)
{
n=read();
for (int i=1;i<=n;i++)//注意多组数据记得清空
E[i].clear();
for (int i=1;i<=n;i++)
W[i]=read();
for (int i=1;i<=n;i++)
D[i]=read();
for (int i=1;i<n;i++)
{
int x=read(),y=read(),w=read();
E[x].push_back((Edge){y,w});
E[y].push_back((Edge){x,w});
}
//cout<<"Read_end"<<endl;
dfs(1,1);
cout<<Best[1]<<endl;
}
return 0;
}
int read()
{
int x=0;
int k=1;
char ch=getchar();
while (((ch<'0')||(ch>'9'))&&(ch!='-'))
ch=getchar();
if (ch=='-')
{
k=-1;
ch=getchar();
}
while ((ch>='0')&&(ch<='9'))
{
x=x*10+ch-48;
ch=getchar();
}
return x*k;
}
void dfs(int u,int father)
{
for (int i=0;i<E[u].size();i++)//首先把所有子节点的值算出来
{
int v=E[u][i].v;
if (v==father)
continue;
dfs(v,u);
}
memset(Dist,-1,sizeof(Dist));//临时用bfs求出u到所有点的距离
while (!Q.empty())
Q.pop();
Dist[u]=0;
Q.push(u);
do
{
int uu=Q.front();
Q.pop();
for (int i=0;i<E[uu].size();i++)
{
int v=E[uu][i].v;
if (Dist[v]==-1)
{
Dist[v]=Dist[uu]+E[uu][i].w;
Q.push(v);
}
}
}
while (!Q.empty());
Best[u]=inf;//因为要求最小,所以初值为无穷大
for (int build=1;build<=n;build++)
{
if (Dist[build]<=D[u])
{
F[u][build]=W[build];
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i].v;
if (v==father)
continue;
F[u][build]+=min(Best[v],F[v][build]-W[build]);//统计u的子节点v
}
Best[u]=min(Best[u],F[u][build]);//用刚计算出来的F[u][build]更新Best[u]
}
else//若build无法覆盖u,则置为无穷大
F[u][build]=inf;
}
return;
}
POJ 2152 fire / SCU 2977 fire(树型动态规划)的更多相关文章
- POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- 树型动态规划(树形dp)
树型动态规划就是在“树”的数据结构上的动态规划,树型动态规划是建立在树上的,所以有二个方向: 1.根—>叶:这种题目基本上碰不到 2.叶->根:根的子节点传递有用的信息给根,完后根得出最优 ...
- CJOJ 1010【NOIP2003】加分二叉树 / Luogu 1040 加分二叉树(树型动态规划)
CJOJ 1010[NOIP2003]加分二叉树 / Luogu 1040 加分二叉树(树型动态规划) Description 设 一个 n 个节点的二叉树 tree 的中序遍历为( 1,2,3,-, ...
- CJOJ 2171 火车站开饭店(树型动态规划)
CJOJ 2171 火车站开饭店(树型动态规划) Description 政府邀请了你在火车站开饭店,但不允许同时在两个相连的火车站开.任意两个火车站有且只有一条路径,每个火车站最多有 50 个和它相 ...
随机推荐
- TCP:三次握手、四次握手、backlog及其他
TCP是什么 首先看一下OSI七层模型: 然后数据从应用层发下来,会在每一层都加上头部信息进行封装,然后再发送到数据接收端,这个基本的流程中每个数据都会经过数据的封装和解封的过程,流程如下图所示: 在 ...
- Maven测试篇
maven的生命周期: 讲解Maven测试篇之前将首先介绍一下Maven生命周期的相关概念,如果你熟知这部分概念可以略过此小节内容. 大多数时候,我们在构建一个项目时,不外乎是对其进行清理.编译.测 ...
- javascript基础-闭包
原理 函数里包含函数,即闭包. 包含函数的结果是,子函数会挟持父函数的活动对象.子函数在访问一个变量时,先读自身的活动对象,是否包含此变量,没有从父函数里找,还没有,去祖函数,层层回溯,直到windo ...
- nodeJS之fs文件系统
前面的话 fs文件系统用于对系统文件及目录进行读写操作,本文将详细介绍js文件系统 概述 文件 I/O 是由简单封装的标准 POSIX 函数提供的. 通过 require('fs') 使用该模块. 所 ...
- 一起学习java
一.Servlet学习 下面是Servlet一个整体的继承结构 首先说一下Servlet这个接口这个主要包含的有init,service,destroy等方法,这里主要介绍这3个 ...
- 生成JSON数据--fastjson(阿里)方法
fastjson(阿里)方法生成JSON数据: 与Gson类似,创建相应类,再使用JSON.toJSONString()添加对象 要求:生成如下JSON数据 1.{"age":3, ...
- FPGA设计思想(持续更新)
一. 流水线设计 将原本一个时钟周期完成的较大的组合逻辑通过合理的切割后分由多个时钟周期完成.该部分逻辑运行的时钟频率会有明显对的提升,提高系统的性能用面积换速度 一个流水线设计需要4个步骤完成一个数 ...
- python之numpy库[1]
python-numpy python中的数据 一维数据 用列表和集合表示 数组与列表的关系 列表:数据类型可以不同 数组:数据类型可以相同 多维数据 用列表表示 高维数据 用字典表示 高维数据仅利用 ...
- DDD理论学习系列(5)-- 统一建模语言
DDD理论学习系列--案例及目录 1.引言 上一节讲解了领域模型,领域模型主要是将业务中涉及到的概念以面向对象的思想进行抽象,抽象出实体对象,确定实体所对应的方法和属性,以及实体之间的关系.然后将这些 ...
- RabbitMQ分布式消息队列服务器(一、Windows下安装和部署)
RabbitMQ消息队列服务器在Windows下的安装和部署-> 一.Erlang语言环境的搭建 RabbitMQ开源消息队列服务是使用Erlang语言开发的,因此我们要使用他就必须先进行Erl ...