POJ 2152 经典树形dp

Fire
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 972 Accepted: 463

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


题意:有n个地区,构成一棵树,由于容易发生火灾,因此需要修消防站来防止火灾发生,在每一个地区修建消防站需要的花费,以及假如这个地区没有消防站,最近的消防站
与该地区的最大距离,求修建消防站的最小花费。
好难的树形dp。看了别人的题解一下午,终于看懂了,dp[i][j]表示以在j点有消防站时,i为根的所有子树收到消防站的保护所需的最小花费,best[i]表示
以i为根的子树收到消防站保护所需的最小花费,树形dp的同时处理当前节点到所有点的距离,然后就可以进行状态转移了。
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-4 19:07:09
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 1000000000;
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=1010;
int dist[maxn],head[maxn],tol,dis[maxn],weight[maxn],n,best[maxn],dp[maxn][maxn];
struct node{
	int next,to,val;
}edge[3*maxn];
void add(int u,int v,int val){
		edge[tol].to=v;
		edge[tol].next=head[u];
		edge[tol].val=val;
		head[u]=tol++;
}
void bfs(int u)
{
    for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].to;
		if(dist[v]!=-1)continue;
		dist[v]=dist[u]+edge[i].val;
		bfs(v);
	}
}
void dfs(int u,int fa){
	for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].to;
		if(v==fa)continue;
		dfs(v,u);
	}
	for(int i=1;i<=n;i++)dist[i]=-1;
	dist[u]=0;bfs(u);
	best[u]=INF;
	for(int i=1;i<=n;i++)
	{
		if(dist[i]>dis[u]){
			dp[u][i]=INF;
		}
		else 
		{
			dp[u][i]=weight[i];
			for(int j=head[u];j!=-1;j=edge[j].next){
				int v=edge[j].to;
				if(v==fa)continue;
				dp[u][i]+=min(best[v],dp[v][i]-weight[i]);
			}
			best[u]=min(best[u],dp[u][i]);
		}
	}
}
int main()
{
   //  freopen("data.txt","r",stdin);
     //freopen("data.out","w",stdout);
     int i,j,k,m,T;
	 scanf("%d",&T);
	 while(T--){
		 scanf("%d",&n);
		 memset(head,-1,sizeof(head));tol=0;
		 for(i=1;i<=n;i++)
			 scanf("%d",&weight[i]);
		 for(i=1;i<=n;i++)
			 scanf("%d",&dis[i]);
		 for(i=1;i<n;i++){
			 scanf("%d%d%d",&j,&k,&m);
			 add(j,k,m);
			 add(k,j,m);
		 }
		 dfs(1,-1);
		 cout<<best[1]<<endl;
	 }
     return 0;
}



 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值