【POJ 2152】 Fire (树形DP)
FireDescription
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 2Sample Output
2
1
2
2
3Source
POJ Monthly,Lou Tiancheng

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 1010 struct node
{
int x,y,c,next;
}t[*Maxn];int len;
int first[Maxn];
int n,w[Maxn],d[Maxn]; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;} int dis[Maxn][Maxn]; void get_dis(int st,int x,int f)
{
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
int y=t[i].y;
dis[st][y]=dis[st][x]+t[i].c;
get_dis(st,y,x);
}
} int f[Maxn][Maxn],g[Maxn];
void ffind(int x,int fa)
{
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
ffind(y,x);
}
for(int i=;i<=n;i++) if(dis[x][i]<=d[x])
{
f[x][i]=w[i];
for(int j=first[x];j;j=t[j].next) if(t[j].y!=fa)
{
int y=t[j].y;
f[x][i]+=mymin(f[y][i]-w[i],g[y]);
}
g[x]=mymin(g[x],f[x][i]);
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
len=;
memset(first,,sizeof(first));
for(int i=;i<=n;i++) scanf("%d",&w[i]);
for(int i=;i<=n;i++) scanf("%d",&d[i]);
for(int i=;i<n;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}
for(int i=;i<=n;i++)
{
dis[i][i]=;
get_dis(i,i,);
}
memset(f,,sizeof(f));
memset(g,,sizeof(g));
ffind(,);
printf("%d\n",g[]);
}
return ;
}
[POJ 2152]
2016-10-17 09:06:04
【POJ 2152】 Fire (树形DP)的更多相关文章
- POJ 2152 Fire(树形DP)
题意: 思路:令F[i][j]表示 的最小费用.Best[i]表示以i为根节点的子树多有节点都找到负责消防站的最小费用. 好难的题... #include<algorithm> #incl ...
- POJ 2152 fire / SCU 2977 fire(树型动态规划)
POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...
- POJ 2152 Fire (树形DP,经典)
题意:给定一棵n个节点的树,要在某些点上建设消防站,使得所有点都能够通过某个消防站解决消防问题,但是每个点的建站费用不同,能够保证该点安全的消防站的距离上限也不同.给定每个点的建站费用以及最远的消防站 ...
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- Apple Tree POJ - 2486 (树形dp)
题目链接: D - 树形dp POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...
- Anniversary party POJ - 2342 (树形DP)
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
- POJ 3107.Godfather 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
- POJ 3342 (树形DP)
题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...
- POJ 2342 (树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3863 Accepted: 2172 ...
- POJ 2152 Fire
算是我的第一个树形DP 的题: 题目意思:N个城市形成树状结构.现在建立一些消防站在某些城市:每个城市有两个树形cost(在这个城市建立消防站的花费),limit : 我们要是每个城镇都是安全的:就是 ...
随机推荐
- 5.CentOS6.6安装git
额,因为公司的项目存放在gitlab上,所以要求员工必须会使用git 这里简单说下git在 linux下的安装,使用的说明,我会单开一篇文章来写 1.首先卸载掉CentOS6.6自带的1.7.1版本的 ...
- maven主仓库中找不到restlet的解决办法
解决办法: 修改Pom.xml 增加 <repositories> <repository> <id>maven-rest ...
- 使用modelsim仿真DDR3时编译出错的解决方法
Modelsim 10.1c release note sates as : Product Changes in 10.1c Release 10.1b introduced a new error ...
- webbreswer
为了帮助网友解决"怎么用C#的webBrowser模拟点击页面上的标签"相关的问题,中国学网通过互联网对"怎么用C#的webBrowser模拟点击页面上的标签" ...
- tomcat7.0 的配置
一.安装JDK 1.7 1.添加环境变量:在 我的电脑->属性->高级->环境变量 2.新建系统变量,变量名:JAVA_HOME 变量值:C:\Program Files\Java\ ...
- 盘点 Github 所用到的开源项目
http://www.php100.com/html/it/mobile/2014/0401/6736.html 在致力于开源事业的同时,Github也使用一些非常优秀的开源项目的来打造自己的平台与服 ...
- ScriptManager的用法
资料中如实是说: 1, ScriptManager(脚本控制器)是asp.net ajax存在的基础. 2, 一个页面只允许有一个ScriptManager,并且放在其他ajax控件的前面 ...
- C#读取Excel表中的数据时,为何有些行的字段内容读取不到
转载:http://bbs.csdn.net/topics/360220285 1.当某列数据中含有混合类型时,在.NET中使用Microsoft.Jet.OLEDB.4.0来读取Excel文件造成数 ...
- java_UML:继承/泛化、实现、依赖、关联、聚合、组合的联系与区别 (2016-07-12)
分别介绍这几种关系: UML关系:继承(泛化).实现.依赖.关联.聚合.组合的联系与区别 一.表示符号上的区别 二.具体区别与联系 1. 继承/泛化(Generalization) [泛化关系]:是一 ...
- UVA 11729 - Commando War(贪心 相邻交换法)
Commando War There is a war and it doesn't look very promising for your country. Now it's time to ac ...