zoukankan      html  css  js  c++  java
  • ACdream 1032 Component

    Component

    Time Limit: 5000ms
    Memory Limit: 64000KB
    This problem will be judged on ACdream. Original ID: 1032
    64-bit integer IO format: %lld      Java class name: (No Java Yet)
     

    Given a tree with weight assigned to nodes, find out minimum total weight connected component with fixed number of node.

     

    Input

    The first line contains a single integer n.

    The second line contains n integers $w_1,w_2,…,w_n. w_i$ denotes the weight of the node i.

    The following (n−1) lines with two integers ai and bi, which denote the edge between ai and bi.

    Note that the nodes are labled by $1,2,…,n.$

    $(1leq nleq 2⋅10^3,1leq w_ileq 10^5)$

    Output

    $n$ integers $c_1,c_2,…,c_n. c_i$ stands for the minimum total weight component with i nodes.

    Sample Input

    3 
    1 2 3
    1 2
    2 3

    Sample Output

    1 3 6

    Source

     
    解题:树形dp
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 2010;
     4 vector<int>g[maxn];
     5 int val[maxn],dp[maxn][maxn],n,son[maxn],ans[maxn];
     6 void dfs(int u,int fa){
     7     dp[u][1] = val[u];
     8     dp[u][0] = 0;
     9     son[u] = 1;
    10     for(int i = g[u].size()-1; i >= 0; --i){
    11         if(g[u][i] == fa) continue;
    12         dfs(g[u][i],u);
    13         son[u] += son[g[u][i]];
    14         for(int j = son[u]; j > 0; --j){
    15             for(int k = 1; k <= j; ++k)
    16                 dp[u][j] = min(dp[u][j],dp[g[u][i]][j - k] + dp[u][k]);
    17         }
    18     }
    19     for(int i = son[u]; i >= 0; --i)
    20         ans[i] = min(ans[i],dp[u][i]);
    21 }
    22 int main(){
    23     while(~scanf("%d",&n)){
    24         for(int i = 0; i < maxn; ++i) g[i].clear();
    25         for(int i = 1; i <= n; ++i) scanf("%d",val + i);
    26         memset(dp,0x3f,sizeof dp);
    27         memset(ans,0x3f,sizeof ans);
    28         for(int i = 1,u,v; i < n; ++i){
    29             scanf("%d%d",&u,&v);
    30             g[u].push_back(v);
    31             g[v].push_back(u);
    32         }
    33         dfs(1,-1);
    34         for(int i = 1; i <= n; ++i)
    35             printf("%d%c",ans[i],i == n?'
    ':' ');
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    Servlet 06: jar包引入和web配置文件创建
    Servlet 05: Servlet生命周期方法
    Servlet 04: 什么是重定向
    Servlet 03: 通过xml配置文件配置servlet访问路径
    080 面向过程编程
    079 内置函数
    078 匿名函数
    077 递归
    076 生成器
    075 字典生成式
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4816582.html
Copyright © 2011-2022 走看看