zoukankan      html  css  js  c++  java
  • URAL 1018 Binary Apple Tree(树DP)

    Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enumerate by integers the root of binary apple tree, points of branching and the ends of twigs. This way we may distinguish different branches by their ending points. We will assume that root of tree always is numbered by 1 and all numbers used for enumerating are numbered in range from 1 to N, where N is the total number of all enumerated points. For instance in the picture below N is equal to 5. Here is an example of an enumerated tree with four branches:
    2   5
      / 
      3   4
        /
        1
    
    As you may know it's not convenient to pick an apples from a tree when there are too much of branches. That's why some of them should be removed from a tree. But you are interested in removing branches in the way of minimal loss of apples. So your are given amounts of apples on a branches and amount of branches that should be preserved. Your task is to determine how many apples can remain on a tree after removing of excessive branches.

    Input

    First line of input contains two numbers: N and Q (2 ≤ N ≤ 100; 1 ≤ Q ≤ N − 1). N denotes the number of enumerated points in a tree. Q denotes amount of branches that should be preserved. NextN − 1 lines contains descriptions of branches. Each description consists of a three integer numbers divided by spaces. The first two of them define branch by it's ending points. The third number defines the number of apples on this branch. You may assume that no branch contains more than 30000 apples.

    Output

    Output should contain the only number — amount of apples that can be preserved. And don't forget to preserve tree's root ;-)
     
    题目大意:有一颗以1为根的二叉树,有n个点,每条边有一个边权,问保留Q条边,如何使这Q条边边权和最大,注意这Q条边必须连着点1。
    思路:可以参考2009年IOI的论文《浅谈几类背包问题》,复杂度为O(NQ)。
     
    代码(31ms):
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int MAXN = 110;
     8 
     9 int head[MAXN], ecnt;
    10 int to[MAXN << 1], next[MAXN << 1], weight[MAXN << 1];
    11 int dp[MAXN][MAXN];
    12 int n, m;
    13 
    14 void init() {
    15     memset(head, -1, sizeof(head));
    16     ecnt = 0;
    17 }
    18 
    19 void add_edge(int u, int v, int c) {
    20     to[ecnt] = v; weight[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
    21     to[ecnt] = u; weight[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
    22 }
    23 
    24 inline void update_max(int &a, int b) {
    25     if(a < b) a = b;
    26 }
    27 
    28 void dfs(int f, int u, int C) {
    29     for(int p = head[u]; ~p; p = next[p]) {
    30         int &v = to[p];
    31         if(v == f) continue;
    32         for(int i = 0; i <= C - 1; ++i) dp[v][i] = dp[u][i] + weight[p];
    33         dfs(u, v, C - 1);
    34         for(int i = 1; i <= C; ++i)
    35             update_max(dp[u][i], dp[v][i - 1]);
    36     }
    37 }
    38 
    39 int main() {
    40     scanf("%d%d", &n, &m);
    41     init();
    42     for(int i = 1; i < n; ++i) {
    43         int u, v, c;
    44         scanf("%d%d%d", &u, &v, &c);
    45         add_edge(u, v, c);
    46     }
    47     dfs(0, 1, m);
    48     printf("%d
    ", dp[1][m]);
    49 }
    View Code
  • 相关阅读:
    Delphi 字符串操作
    SQL Browser (数据浏览器)
    .NET 3.5和VS 2008中的ASP.NET AJAX(转帖)
    delphi 最快速编码 URLDecode URLEncode
    Delphi 2007 如何安装控件
    Delphi TWebBrowser编程简述(转帖)
    delphi TStringList的用法
    Javascript+xmlhttp调用Webservice以及注意事项
    使用VSS 的Shadow folder的一点问题
    delphi 如何将XML格式的字符串导入ClientDataSet中
  • 原文地址:https://www.cnblogs.com/oyking/p/3538405.html
Copyright © 2011-2022 走看看