URAL 1018 Binary Apple Tree

Description

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.  Qdenotes 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

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

题目大意:给你一棵树,每条边有边权, 以根节点开始保留Q条边, 使边权总和最大。(一定要保留根节点)
最基础的树形DP,只不过DP的是边权而已。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int n;
bool visit[110];
int dp[110][110];
int v[110][110];
bool map[110][110];

void dfs(int x)
{int i,j,k;
    visit[x]=1;
    for(i=1;i<=n;i++)
    {
        if(map[x][i]&&!visit[i])
        {
            dp[i][1]=v[x][i];
            dfs(i);

            for(j=n;j>1;j--)
                for(k=1;j-k>=1;k++)
                    dp[x][j]=max(dp[x][j],dp[x][j-k]+dp[i][k]);
        }
    }
}

int main()
{int m,x,y,z;
    while(cin>>n>>m)
    {
        memset(v,0,sizeof(v));
        memset(dp,0,sizeof(dp));
        memset(map,0,sizeof(map));
        memset(visit,0,sizeof(visit));

        for(int i=1;i<n;i++)
        {
           scanf("%d%d%d",&x,&y,&z);
           v[x][y]=v[y][x]=z;
           map[x][y]=map[y][x]=1;
        }
        dfs(1);
        cout<<dp[1][m+1]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值