您的位置:首页 > 移动开发

URAL 1018 Binary Apple Tree 简单树形背包

2014-02-11 02:40 495 查看
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. 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
inputoutput
5 2
1 3 1
1 4 10
2 3 20
3 5 20

21

题意:一颗n个节点的树,去掉一些边,使得剩下m条边,使得边权和最大,1为根节点,必须保留。

简单树形背包,先dfs出以各个节点为根的子树的大小,然后背包求最大值。

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-11 2:29:40
File Name :4.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=130;
int head[maxn],tol,dp[maxn][maxn],size[maxn],m,n;
struct node{
int next,to,val;
node(){};
node(int _next,int _to,int _val):next(_next),to(_to),val(_val){}
}edge[10*maxn*maxn];
void add(int u,int v,int w){
edge[tol]=node(head[u],v,w);
head[u]=tol++;
}
void dfs(int u,int fa){
size[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa)continue;
dfs(v,u);
size[u]+=size[v];
}
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa)continue;
for(int j=size[u];j>1;j--)
for(int k=1;k<j;k++)
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]+edge[i].val);
}
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
while(~scanf("%d%d",&n,&m)){
memset(head,-1,sizeof(head));tol=0;
for(int i=1;i<n;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dfs(1,-1);
printf("%d\n",dp[1][m+1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: