博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binary Apple Tree_ural1018_树状dp
阅读量:5147 次
发布时间:2019-06-13

本文共 3041 字,大约阅读时间需要 10 分钟。

Description

Let's imagine how apple tree looks inbinary computer world. You're right, it looks just like a binary tree, i.e. anybiparous branch splits up to exactly two new branches. We will

 enumerate byintegers the root of binary apple tree, points of branching and the ends oftwigs. 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 usedfor enumerating are numbered in range from 1 to N, where N is the total numberof 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 pickan apples from a tree when there are too much of branches. That's why some ofthem should be removed from a tree. But you are

 interested in removing branchesin the way of minimal loss of apples. So your are given amounts of apples on abranches and amount of branches that should be preserved. Your

 task is todetermine how many apples can remain on a tree after removing of excessivebranches.

Input

First line of input contains two numbers: Nand 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 containsdescriptions of branches. Each description consists of a three integer numbersdivided 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 assumethat 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'sroot ;-)

SampleInput

5 2

1 3 1

1 4 10

2 3 20

3 5 20

SampleOutput

21

 

题目大意:

有一颗拥有n关系的带权二叉树要剪去q根枝条求整棵树的最大权值和。

 

思路:

因为是求最优值所以想到dp,因为是赤裸裸的二叉树所以想到赤裸裸的树状dp,因为是赤裸裸的树状dp所以放上赤裸裸的状态转移方程。

f[i,j]:=max(f[i.left,k]+a[I,i.left],f[i.right,j-k]+a[I,i.right]),0<=k<=j

其中f[I,j]表示以i为根节点,剪去j根枝条的最优值,a[I,j]表示连接I、j枝条上的权值。F[1,q]即所求答案。注意要在开始转换成一颗二叉树。

 

源代码/pas

 

typetree=record  l,r:longint;end;var  n,q:longint;  a,f,v:array[0..100,0..100]of longint;  t:array[0..100]of tree;function max(x,y:longint):longint;begin  if x>y then  max:=x  else  max:=y;end;procedure init;var  i,x,y,z:longint;begin  readln(n,q);  for i:=1 to n-1 do  begin    readln(x,y,z);    a[x,y]:=z;    a[y,x]:=z;    v[x,y]:=1;    v[y,x]:=1;  end;end;procedure create(x:longint);var  i:longint;begin  for i:=1 to n do  if (v[x,i]<>0) then  begin    if (t[x].l=0) then t[x].l:=i    else    if (t[x].r=0) then t[x].r:=i;    v[i,x]:=0;    v[x,i]:=0;    create(i);  end;end;procedure dp;var  i,j,k:Longint;begin  for i:=1 to n do  f[i,1]:=max(a[i,t[i].l],a[i,t[i].r]);  for j:=2 to q do  for i:=1 to n do  begin    f[i,j]:=max(a[i,t[i].r]+f[t[i].r,j-1],a[i,t[i].l]+f[t[i].l,j-1]);    for k:=0 to j-2 do    f[i,j]:=max(f[i,j],f[t[i].l,k]+f[t[i].r,j-2-k]+a[i,t[i].l]+a[i,t[i].r]);  end;end;begin  init;  create(1);  dp;  writeln(f[1,q]);end.

转载于:https://www.cnblogs.com/olahiuj/p/5781334.html

你可能感兴趣的文章
java字节流复制文件
查看>>
重载和覆盖
查看>>
实验二 进程调度预备
查看>>
7zip在DOS命令行用法总结
查看>>
Xcode开发 字符串用法
查看>>
在IIS中实现JSP
查看>>
[转载]Meta标签详解
查看>>
File,FileStream,byte[]3者互相转换总结(转)
查看>>
springboot 使用devtools 实现热部署
查看>>
Yahoo网站性能最佳体验的34条黄金守则
查看>>
forward与redirect的区别
查看>>
网络编程之socket
查看>>
Maven pom项目部署
查看>>
Cognos报表验证(添加字段)
查看>>
学术-物理-维空间:一维空间
查看>>
CSS:CSS 实例
查看>>
python-文件读写操作
查看>>
P1129 [ZJOI2007]矩阵游戏 二分图匹配
查看>>
Git 内部原理之 Git 对象哈希
查看>>
Vue中引入TradingView制作K线图
查看>>