Fire - POJ 2152 树形dp

本文介绍了一种复杂度为O(n^2)的树形DP方法,用于解决在一棵树上建设消防站以满足各节点特定距离要求的最小成本问题。通过详细解释算法思路和状态转移方程,提供了实现此问题解决方案的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fire
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1098 Accepted: 546

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

题意:在一棵树上的一些节点安消防栓,每个节点都有花费,和最近的消防栓要求的距离,求满足所有点要求的最小花费。

思路:复杂度为O(n^2)的树形DP.因为要依赖其他站点,所以不仅仅只从子树中获取信息,也可能从父亲结点,兄弟结点获取信息,所以在计算每个点时首先想到要枚举,因为n特别小,允许我们枚举。设dp[i][j]表示i点及其子树都符合情况下i点依赖j点的最小花费,有了这个似乎还不够,再开个一维数组best,best[i]表示以i为根的子树符合题目要求的最小花费。这样状态转移方程就是dp[i][j] = cost[j] + sum(min(dp[k][j]-cost[j],best[k])) (k为i的子节点,j为我们枚举的n个点),因为i的每个子节点可以和i一样依赖j结点,那么花费是dp[k][j]-cost[j],或者依赖以k为根的树中的某点,花费是best[k],最后再加上cost[j],因为要在j结点建站所以要增加花费。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int T,t,n,link[1010][1010],dis[1010][1010],W[1010],D[1010],vc_len[1010],dp[1010][1010],best[1010];
vector <int> vc[1010];
void dfs(int g,int f,int u,int len)
{
    link[g][u]=t;
    //printf("link %d %d\n",g,u);
    for(int i=0;i<vc_len[u];i++)
       if(vc[u][i]!=f && len-dis[u][vc[u][i]]>=0)
         dfs(g,u,vc[u][i],len-dis[u][vc[u][i]]);
}
void dfs2(int f,int u)
{
    int i,j,k,v;
    for(i=0;i<vc_len[u];i++)
    {
        v=vc[u][i];
        if(v==f)
          continue;
        dfs2(u,v);
    }
    for(j=1;j<=n;j++)
    {
        if(link[u][j]!=t)
          continue;
        dp[u][j]=W[j];
        for(k=0;k<vc_len[u];k++)
        {
            v=vc[u][k];
            if(v==f)
              continue;
            if(link[v][j]==t)
              dp[u][j]+=min(dp[v][j]-W[j],best[v]);
            else
              dp[u][j]+=best[v];
        }
        best[u]=min(best[u],dp[u][j]);
    }
}
int main()
{
    int i,j,k,u,v,len;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
           vc[i].clear();
        for(i=1;i<=n;i++)
           best[i]=1000000000;
        for(i=1;i<=n;i++)
           scanf("%d",&W[i]);
        for(i=1;i<=n;i++)
           scanf("%d",&D[i]);
        for(i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&len);
            vc[u].push_back(v);
            vc[v].push_back(u);
            dis[u][v]=dis[v][u]=len;
        }
        for(i=1;i<=n;i++)
           vc_len[i]=vc[i].size();
        for(i=1;i<=n;i++)
           dfs(i,0,i,D[i]);
        dfs2(0,1);
        printf("%d\n",best[1]);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值