码迷,mamicode.com
首页 > 其他好文 > 详细

(树形DP) ural 1018

时间:2015-05-28 00:19:26      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

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 ≤ 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 ;-)

Sample

inputoutput
5 2
1 3 1
1 4 10
2 3 20
3 5 20
21
Problem Source: Ural State University Internal Contest ‘99 #2 
 
 
就是求以1为根节点的书中有q条边的最大权值。。。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
vector<int> e[105],w[105];
int n,q,dp[105][105],siz[105];
void dfs(int u,int father)
{
    siz[u]=1;
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        int val=w[u][i];
        if(v==father)
            continue;
        dfs(v,u);
        siz[u]+=siz[v];
        for(int j=siz[u];j>=1;j--)
        {
            for(int k=1;k<j&&k<=siz[v];k++)
            {
                dp[u][j]=max(dp[u][j],dp[v][k]+dp[u][j-k]+val);
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=1;i<n;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        e[x].push_back(y);
        e[y].push_back(x);
        w[x].push_back(z);
        w[y].push_back(z);
    }
    dfs(1,-1);
    printf("%d\n",dp[1][1+q]);
    return 0;
}

  

(树形DP) ural 1018

标签:

原文地址:http://www.cnblogs.com/water-full/p/4534613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!