程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ural 1018 Binary Apple Tree

ural 1018 Binary Apple Tree

編輯:C++入門知識

ural 1018 Binary Apple Tree


1018. Binary Apple Tree

Time limit: 1.0 second
Memory limit: 64 MB 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 ≤ QN ? 1). N denotes the number of enumerated points in a tree. Q denotes amount of branches that should be preserved. Next N ? 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 ;-)

Sample

input output
5 2
1 3 1
1 4 10
2 3 20
3 5 20
21

剛開始學樹狀dp,歡迎來噴。

有一顆蘋果樹,它的樹枝符合完全二叉樹。每個樹枝上有一些蘋果。節點標號為1~N(1為根節點)。現在因為樹枝太多了,需要保留q根樹枝。問最多能保留多少蘋果。

題目給出的數據並沒有按照跟節點到子節點的順序給出,如何建樹便一直困擾了我好久(戰五渣-。-)。 最終借助結構體存放兩個兒子的下標比較優美地建出來了。

接下來講轉移。顯然我們不光要考慮節點,還要考慮以該節點為根的樹選擇了多少條邊。

dp[t][k] : 已 t 為根的子樹保留k條邊(樹枝)最多能保留多少條蘋果。

由此我們可以發現如果在某個節點保留了k條邊,有以下兩種情況。

  1:只選擇一棵子樹。那麼dp[t][k] = max(dp[t.l][k-1] + w[t][t.l], dp[t.r][k-1] + w[t][t.r]) //意思是在該子樹(左或右)選擇k-1條邊,並加上根節點到該子樹的邊。

  2:選擇兩顆子樹。那麼就存在兩條固定的邊(根節點到其左右孩子的邊),dp[t][k] = w[t][t.l] +w[t][t.r] + max(dp[t.l][j] + dp[t.r][k - j - 2]) /*(j : 0 ~ K-2)*/ 。

   即左子樹和右子樹總共選擇k-2條邊。

由此,輸出dp[1][q]就好。

#include 
#include 
#include 
#include 
using namespace std;

struct Node {
    int l, r;
}T[105];

int n, q;
int dp[105][105], map[105][105];

void buildTree(int t) {//建樹
    int flag = 0; 
    for (int i=0; i<=n; i++) {
        if (map[t][i] && !T[i].l && !T[t].r) {//!T[i].l不為父節點
            if (!T[t].l) T[t].l = i;
            else T[t].r = i;
            flag = 1;
        }
    }
    if (flag) {
        buildTree(T[t].l);
        buildTree(T[t].r);
    }
}

void Tdp (int s) {
    if (T[s].l == 0) return ;
    Tdp(T[s].l);
    Tdp(T[s].r);
    for (int i=1; i<=q; i++) {//轉移
        dp[s][i] = max(dp[T[s].l][i-1] + map[s][T[s].l], dp[T[s].r][i-1] + map[s][T[s].r]);
        for (int j=0; j> n >> q ;
    int a, b, c;
    for (int i=1; i> a >> b >> c;
        map[a][b] = map[b][a] = c;
    }
    buildTree(1);
    Tdp(1);
    cout << dp[1][q] <

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved