URAL 1018 (金典树形DP)

连接: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. 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


题目意思: 
有一棵苹果树,苹果树的是一棵二叉树,共N个节点,树节点编号为1~N,编号为1的节点为树根,边可理解为树的分枝,每个分支都长着若干个苹果,现在要要求减去若干个分支,保留M个分支,要求这M个分支的苹果数量最多。
二叉苹果树:一道金典的树形DP,这题很特殊,因为是二叉树,所以只需要处理左二子,右儿子就可以了,但是我还是想着用一般的树形DP来做这道题,就是不当成二叉树来做。
思路:跟0-1背包思想差不多,在u的儿子v为根节点的子树中选j条边加到u中。
dp[u][k]=max(dp[u][k],dp[u][k-j]+dp[v][j-1]+w)(1<j<=k),w:u与v的边的取值,因为如果在v子树中选边,那么u到v的边必选。



#include<stdio.h>
#include<string.h>
const int N=110;
int dp[N][N],vis[N],head[N],num,m;
struct edge
{
	int st,ed,w,next;
}e[N*4];
void addedge(int x,int y,int w)
{
	e[num].st=x;e[num].ed=y;e[num].w=w;e[num].next=head[x];head[x]=num++;
	e[num].st=y;e[num].ed=x;e[num].w=w;e[num].next=head[y];head[y]=num++;
}
void dfs(int u)
{
	vis[u]=1;
	int i,v,w,j,k,son=0;
	for(i=head[u];i!=-1;i=e[i].next)
	{
		v=e[i].ed;w=e[i].w;
		if(vis[v]==1)continue;
		dfs(v);
		for(k=m;k>=1;k--)//0-1背包
		{
		    for(j=1;j<=k;j++)//在v节点的子树中选择j条边
			 if(dp[u][k]<dp[u][k-j]+dp[v][j-1]+w)//u与v有一条边,所以加上dp[v][j-1],
				dp[u][k]=dp[u][k-j]+dp[v][j-1]+w;
		}
	}
}		
int main()
{
	int i,x,y,w,n;
	while(scanf("%d%d",&n,&m)!=-1)
	{
		memset(head,-1,sizeof(head));
		num=0;
		for(i=1;i<n;i++)
		{
			scanf("%d%d%d",&x,&y,&w);
			addedge(x,y,w);
		}
		memset(vis,0,sizeof(vis));
		memset(dp,0,sizeof(dp));
		dfs(1);	
		printf("%d\n",dp[1][m]);
	}
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值