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

  • 相关阅读:
    二,数据类型与流程控制语句
    一,cmd指令集与变量
    web第九天,浮动与定位
    web第八天,PS切图与float浮动
    web第七天,标签分类
    web第六天,CSS优先级与盒子模型
    web第五天复合样式与选择器
    web第四天,CSS基础
    web第三天 表单与css基础
    装饰器
  • 原文地址:https://www.cnblogs.com/twodog/p/12137500.html
Copyright © 2011-2022 走看看