HDU 6060 RXD and dividing(贪心,思维)

来源:互联网 发布:mac必下的软件 编辑:程序博客网 时间:2024/06/07 05:44

RXD and dividing

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


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

题目分析:首先要知道对于根节点为1的斯坦纳树,其最小值就是每一个结点直接到1的距离之和,排除同一集合中相同路径。那么我们就很清楚了。这道题要分为K个块,对于同一个块来说,肯定点越分散越好。那么怎样做到最分散呢?即:对于每一颗子树,其下的点分布在

dif_i\leq min(k, sz_i)difimin(k,szi)sz_iszi表示子树大小.就可以取得最分散的情况。然后dfs一下就好咯。

代码如下:

/* Author:kzl */#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<vector>using namespace std;typedef long long LL;const int maxx = 1e6+10;struct My{int to,val;My(){}My(int a,int b):to(a),val(b){}};vector<My>ve[maxx];int n,k;int aa,bb,cc;LL sum = 0;int dfs(int num,int p){    int ans = 0;    int sz = ve[num].size();    for(int i=0;i<sz;i++){        int v = ve[num][i].to;        if(v==p)continue;        int x = dfs(v,num);        ans += x;        sum += 1LL * (min(k,x)) * ve[num][i].val;        //cout<<num<<" "<<k<<" "<<x<<" "<<ve[num][i].val<<" "<<sum<<endl;    }    //cout<<num<<" "<<ans+sz<<endl;    return  ans + 1 ;}int main(){while(scanf("%d%d",&n,&k)!=EOF){    for(int i=0;i<=n;i++)ve[i].clear();    for(int i=1;i<n;i++){        scanf("%d%d%d",&aa,&bb,&cc);        ve[aa].push_back(My(bb,cc));        ve[bb].push_back(My(aa,cc));    }    sum = 0;dfs(1,0);    printf("%lld\n",sum);}return 0;}

dif_i\leq min(k, sz_i)difimin(k,szi)sz_iszi表示子树大小
原创粉丝点击