Hdu6060 RXD and dividing(2017多校第3场)



RXD and dividing

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


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个节点的树,要求将2-n号节点分成k部分,然后将每一部分加上节点1,
一个子树的val为最小斯坦纳树,求总的最大val
其实这里的斯坦纳树很简单,就是在原树中找最少的边是的一个集合里的点全部连通
思路:比赛时在那贪心构造搞,搞了半天弄不出,赛后才发现很简单的
先贴一下官方题解:
把1看成整棵树的根. 问题相当于把2\sim n2n每个点一个[1, k][1,k]的标号. 然后根据最小斯坦纳树的定义, (x, fa_x)(x,fax) 这条边的
贡献是 x 子树内不同标号的个数目dif_idifi. 那么显然有dif_i\leq min(k, sz_i)difimin(k,szi)sz_iszi表示子树大小. 可以通过构造让所有dif_idifi
取到最大值. 所以 答案就是
\sum_{x = 2}^{n}{w[x][fa_x] * min(sz_x, k)}x=2nw[x][fax]min(szx,k) 时间复杂度O(n)O(n).
其实很简单,要想和最大,每条边尽可能多的用,而他用的次数是这条边延伸下去的
节点数和k取一个较小值,最后把每条边的权值*用的次数累加即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define LL long long
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
#define MAXN 1000005
#define mem(a,b) memset(a,b,sizeof a)


struct node
{
    int u,v,next;
    LL c;
} tree[2*MAXN];
int s[MAXN],cnt,sum[MAXN];
LL pre[MAXN];

void init()
{
    mem(s,-1);
    cnt=0;
}

void add(int u,int v,LL c)
{
    tree[cnt].u=u;
    tree[cnt].v=v;
    tree[cnt].c=c;
    tree[cnt].next=s[u];
    s[u]=cnt++;

    tree[cnt].u=v;
    tree[cnt].v=u;
    tree[cnt].c=c;
    tree[cnt].next=s[v];
    s[v]=cnt++;

}


void dfs(int k,int fa)
{
    sum[k]=1;
    for(int i=s[k];~i;i=tree[i].next)
    {
        if(tree[i].v==fa)
            continue;
        pre[tree[i].v]=tree[i].c;
        dfs(tree[i].v,k);
        sum[k]+=sum[tree[i].v];
    }
}

int main()
{
    int n,k,u,v;
    LL c;
    while(~scanf("%d%d",&n,&k))
    {
        init();
        for(int i=0; i<n-1; i++)
        {
            scanf("%d%d%lld",&u,&v,&c);
            add(u,v,c);
        }
        mem(pre,0);
        mem(sum,0);
        dfs(1,-1);
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=1LL*pre[i]*min(k,sum[i]);
        }
        printf("%lld\n",ans);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值