zoukankan      html  css  js  c++  java
  • 洛谷P3018 [USACO11MAR]树装饰Tree Decoration

    洛谷P3018 [USACO11MAR]树装饰Tree Decoration
    树形DP
    因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了

     1 #include <bits/stdc++.h>
     2 #define For(i, j, k) for(int i=j; i<=k; i++)
     3 #define Dow(i, j, k) for(int i=j; i>=k; i--)
     4 #define LL long long
     5 using namespace std;
     6 inline int read() {
     7     int x = 0, f = 1;
     8     char ch = getchar();
     9     while(ch<'0'||ch>'9') { if(ch=='-') f = -1; ch = getchar(); }
    10     while(ch>='0'&&ch<='9') { x = x*10+ch-48; ch = getchar(); }
    11     return x * f;
    12 }
    13 
    14 const int N = 1e5+11; 
    15 int n, nedge; 
    16 int Mn[N], fa[N], head[N], need[N]; 
    17 LL f[N], have[N];  
    18 struct edge{
    19     int to, pre; 
    20 }e[N*2];
    21 
    22 inline void add(int x,int y) {
    23     e[++nedge].to = y; 
    24     e[nedge].pre = head[x]; 
    25     head[x] = nedge; 
    26 }
    27 
    28 void dfs(int u) {
    29     for(int i=head[u]; i; i=e[i].pre) {
    30         int v = e[i].to; 
    31         if(v == fa[u]) continue; 
    32         dfs( v ); 
    33         have[u] += have[v]; 
    34         Mn[u] = min(Mn[u], Mn[v]); 
    35         f[u] += f[v]; 
    36     }
    37     if(need[u] > have[u]) {
    38         f[u] = f[u]+1ll*(need[u]-have[u])*Mn[u]; 
    39         have[u] = need[u]; 
    40     }
    41 }
    42 
    43 int main() {
    44     n = read(); 
    45     For(i, 1, n) {
    46         fa[i] = read(); 
    47         need[i] = read(); 
    48         Mn[i] = read(); 
    49         if(fa[i] != -1) add(i, fa[i]), add(fa[i], i); 
    50     }
    51     dfs(1); 
    52     printf("%lld
    ",f[1]); 
    53     return 0; 
    54 }

    转载于:https://www.cnblogs.com/third2333/p/8444558.html

  • 相关阅读:
    git commit之后未submit,rebase之后找不到自己代码的处理方法
    Objective-C语言--属性和实例变量
    synthesize 与dynamic的区别
    isKindOfClass和isMemberOfClass 区别
    python 报错整理
    使用fastJson 来解析 json
    使用Gson 解析json
    android json数据解析
    Android 常用布局
    cocos2dx 学习代码记录
  • 原文地址:https://www.cnblogs.com/twodog/p/12137502.html
Copyright © 2011-2022 走看看