hdu--6060--RXD and dividing

RXD and dividing

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


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 4 1 2 3 2 3 4 2 4 5 2 5 6
 

Sample Output
  
  
27
 

Source


题意:

给定一棵 n 个节点的树,1 为根。现要将节点 2 ~ n 划分为 k 块,使得每一块与 根节点 形成的最小斯坦纳树的 边权值 总和最大。

解题思路:

这题可以转化为每条边对答案的贡献是多少,每条边尽量被多次用到,则这条边的贡献就越大。那么如何使这条边被尽量多的用到?最多可以被用到多少次?就是下面需要考虑的。拿ab这条边来说,要想多次被用到  取决于b的子树中的节点被分进了多少个点集,分进的点集越多,对答案的贡献越大。b子树中的点最多被分到min(sz(i),k)个点集中,枚举每一条边ai,bi,把他们的贡献累加  就是答案。

代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using  namespace std;

struct edge
{
     int v, w;  ///子节点,与子节点路径的权重
};

edge temp;
vector<edge> vec[ 1000005];
int Size[ 1000005];
int w1[ 1000005];

void dfs( int u,  int pre)
{
     ///深搜,求出每个父节点对应子树的大小,以及父节点到各个子节点的路径的权重。
    Size[u] =  1;
     int l = vec[u].size();
     for( int i =  0; i < l; i++)
    {
         int v = vec[u][i].v;  ///下一个要遍历的子节点。
         if(v != pre)
        {
            w1[v] = vec[u][i].w;  ///得出权重
            dfs(v, u);
            Size[u] += Size[v];  ///回溯时,更新子节点我的个数,
        }
    }
}

int main()
{
     int n, k;
     while(scanf( "%d%d", &n, &k) != EOF)
    {
        memset(vec,  0sizeof(vec));
        memset(Size,  0sizeof(Size));
        memset(w1,  0sizeof(w1));
         for( int i =  1; i <= n -  1; i++)
        {
             int u, v, w;
            scanf( "%d%d%d", &u, &v, &w);
            temp.v = v;
            temp.w = w;
            vec[u].push_back(temp);
            temp.v = u;
            vec[v].push_back(temp);
        }
        dfs( 1, - 1);  ///从根节点开始深搜,
         long  long sum =  0;
         for( int i =  2; i <= n; i++)
        {
             ///每条路径,被用过的次数最多为min(Size[i],k),乘上其权重 就是他的贡献,然后进行累加。
            sum += ( long  long)w1[i] * min(Size[i], k);
        }
        printf( "%lld\n", sum);
    }
     return  0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值