您的位置:首页 > 其它

Fire - POJ 2152 树形dp

2014-10-04 14:59 519 查看
Fire

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 1098Accepted: 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]);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: