Hdu 6060 RXD and dividing【思维】

来源:互联网 发布:反恐数据库外泄 编辑:程序博客网 时间:2024/06/06 00:58

RXD and dividing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 22    Accepted Submission(s): 6


Problem Description
RXD has a tree T, with the size of n. Each edge has a cost.
Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T
he wants to divide 2,3,4,5,6,n into k parts S1,S2,S3,Sk,
where Si={2,3,,n} and for all different i,j , we can conclude that SiSj=
Then he calulates res=ki=1f({1}Si).
He wants to maximize the res.
1kn106
the cost of each edge[1,105]
Si might be empty.
f(S) means that you need to choose a couple of edges on the tree to make all the points in S connected, and you need to minimize the sum of the cost of these edges.f(S) is equal to the minimal cost 
 

Input
There are several test cases, please keep reading until EOF.
For each test case, the first line consists of 2 integer n,k, which means the number of the tree nodes , and k means the number of parts.
The next n1 lines consists of 2 integers, a,b,c, means a tree edge (a,b) with cost c.
It is guaranteed that the edges would form a tree.
There are 4 big test cases and 50 small test cases.
small test case means n100.
 

Output
For each test case, output an integer, which means the answer.
 

Sample Input
5 41 2 32 3 42 4 52 5 6
 

Sample Output
27

题目大意:


给一颗N个点的树,让你将2~N共n-1个点分到K个集合当中,使得ΣF(1∪Si)的最大。

定义F(集合)表示在原树中,将这个集合中的各个点连通起来最小的边权值花费。


思路:

因为是求ΣF(1∪Si)的最大,所以我们希望从1节点出发,经过的边次数越多越好。

所以我们希望子树中的节点尽量均分给这K个集合,使得这条边尽量的遍历K次即可。


那么对于一条边(u--->v)权值为w,我们能够贡献的最大价值就是w*min(size【v】,k),这里size【v】表示点v子树中点的个数。


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define ll __int64struct node{    int from;    int to;    int w;    int next;}e[2500000];int head[1050000];ll size[1050000];ll ans;int cont;int n,k;void add(int from,int to,int w){    e[cont].to=to;    e[cont].w=w;    e[cont].next=head[from];    head[from]=cont++;}ll Dfs(int u,int from,ll len){    size[u]=1;    for(int i=head[u];i!=-1;i=e[i].next)    {        int v=e[i].to;        ll w=e[i].w;        if(v==from)continue;        Dfs(v,u,w);        size[u]+=size[v];    }    ans+=len*min((ll)k,size[u]);    return size[u];}int main(){    while(~scanf("%d%d",&n,&k))    {        ans=0;        cont=0;        memset(head,-1,sizeof(head));        memset(size,0,sizeof(size));        for(int i=1;i<=n-1;i++)        {            int x,y;            ll w;            scanf("%d%d%I64d",&x,&y,&w);            add(x,y,w);            add(y,x,w);        }        Dfs(1,-1,0);        printf("%I64d\n",ans);    }}















原创粉丝点击