URAL1018 Binary Apple Tree

來源:互聯網
上載者:User

標籤:

 
Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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

input output
5 21 3 11 4 102 3 203 5 20
21

Source

Problem Source: Ural State University Internal Contest ‘99 #2   樹形DPf[當前結點][所選結點數]=最優解 dp的時候num要多加1,是因為當前結點也得選,才能選子結點。 
 1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=200; 9 struct edge{10     int v,w;11     int nxt;12 }e[mxn];13 int hd[mxn],mct=0;14 void add_edge(int u,int v,int dis){15     e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=dis;hd[u]=mct;16     return;17 }18 int f[mxn][mxn];19 int num[mxn];20 int n,m;21 void dp(int u,int fa){22     int i,j,k;23     num[u]=0;24     for(i=hd[u];i;i=e[i].nxt){25         int v=e[i].v;26         if(v==fa)continue;27         dp(v,u);28         num[u]+=num[v]+1;29         for(j=num[u];j;--j){30             for(k=j;k;k--){31                 f[u][j]=max(f[u][j],f[u][j-k]+f[v][k-1]+e[i].w);32             }33         }34     }35     return;36 }37 int main(){38     scanf("%d%d",&n,&m);39     int i,j;40     int u,v,d;41     for(i=1;i<n;i++){42         scanf("%d%d%d",&u,&v,&d);43         add_edge(u,v,d);44         add_edge(v,u,d);45     }46     dp(1,0);47     printf("%d\n",f[1][m]);48     return 0;49 }

 

URAL1018 Binary Apple Tree

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.