zoukankan      html  css  js  c++  java
  • POJ 2152.Fire 树形dp

    Fire
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 1616   Accepted: 878

    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
    

    Source

    POJ Monthly,Lou Tiancheng
     
    题意一个国家有n个城市,城市之间的道路是树状结构。现在要修一些消防站,在城市建设消防站花费为w[i],每个城市必须离消防站的距离小于d[i]。求建立完善的消防站需要至少花费多少。
    思路:树形dp。dp[u][v]表示u城市依赖v城市建设的消防站。dp[u][v]+=dp[son][v]-w[v],其中son表示u的儿子。ans[u]表示u为根时,最优答案。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<map>
    #include<stack>
    #include<set>
    #include<bitset>
    using namespace std;
    #define PI acos(-1.0)
    #define eps 1e-8
    typedef long long ll;
    typedef pair<int,int > P;
    const int N=1e3+100,M=2e6+100;
    const int inf=0x3f3f3f3f;
    const ll INF=1e13+7,mod=1e9+7;
    struct edge
    {
        int from,to;
        int w;
        int next;
    };
    edge es[M];
    int cnt,head[N];
    int n;
    int w[N],d[N];
    int dist[N][N];
    int dp[N][N],ans[N];
    void init()
    {
        cnt=0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v,int w)
    {
        cnt++;
        es[cnt].from=u,es[cnt].to=v;
        es[cnt].w=w;
        es[cnt].next=head[u];
        head[u]=cnt;
    }
    void bfs(int s)
    {
        queue<int>q;
        dist[s][s]=0;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int i=head[u]; i!=-1; i=es[i].next)
            {
                edge e=es[i];
                if(dist[s][e.to]>dist[s][u]+e.w)
                {
                    dist[s][e.to]=dist[s][u]+e.w;
                    q.push(e.to);
                }
            }
        }
    }
    void dfs(int u,int fa)
    {
        for(int i=head[u]; i!=-1; i=es[i].next)
        {
            int v=es[i].to;
            if(v==fa) continue;
            dfs(v,u);
        }
        for(int v=1; v<=n; v++)
        {
            if(dist[u][v]>d[u]) continue;
            dp[u][v]=w[v];
            for(int i=head[u]; i!=-1; i=es[i].next)
            {
                edge e=es[i];
                if(e.to==fa) continue;
                dp[u][v]+=min(dp[e.to][v]-w[v],ans[e.to]);
            }
            ans[u]=min(ans[u],dp[u][v]);
        }
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            init();
            scanf("%d",&n);
            for(int i=1; i<=n; i++) scanf("%d",&w[i]);
            for(int i=1; i<=n; i++) scanf("%d",&d[i]);
            for(int i=1; i<n; i++)
            {
                int u,v,l;
                scanf("%d%d%d",&u,&v,&l);
                addedge(u,v,l),addedge(v,u,l);
            }
            memset(dist,inf,sizeof(dist));
            memset(dp,inf,sizeof(dp));
            memset(ans,inf,sizeof(ans));
            for(int i=1; i<=n; i++) bfs(i);
            dfs(1,0);
            printf("%d
    ",ans[1]);
        }
        return 0;
    }
    树形dp
  • 相关阅读:
    POJ 1182 食物链 +POJ 2492 A Bug's Life 种类并查集
    Codeforces 884C Bertown Subway dfs判环
    HDU 2102 A计划 bfs
    Codeforces Round #442 (Div. 2) 877E
    Codeforces Round #443 (Div. 2) B. Table Tennis
    Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks
    HDU 1495 非常可乐 BFS/数论
    Codeforces Round #442 (Div. 2) B. Nikita and string DP
    51Nod 1102 面积最大的矩形 +1272 最大距离 单调栈
    HDU 1286 找新朋友
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7631663.html
Copyright © 2011-2022 走看看