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

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;
}




發佈了236 篇原創文章 · 獲贊 3 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章