2017杭电多校第三场1005 RXD and dividing(最大化k个斯坦纳树分块的最大权值和)HDU6060

21 篇文章 0 订阅
1 篇文章 0 订阅

RXD and dividing

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


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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6066  6065  6064  6063  6062 

题意:给出一棵有n个顶点的树,然后将2~n号顶点分成k块,求1号顶点到分成k块后各个顶点的最大权值和。
解题思路:给出一棵树,最大化k个斯坦纳树。
 将2~n的结点编号{1~k},每条边的权乘以其下子树的大小与k的较小值。
因为要使权值和最大,那么边都要尽量的多算几次,因为每条边最多算min(其下子树的大小 , k)这么多次,在每条边算最多次数时,相应的那个节点以下的其他节点被各自分到k个不同的块里面,所以最终的答案就是每条边的权乘以其下子树的大小与k的较小值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL MOD=1e9+7;
const int MAXN=1e6+7;
int head[MAXN],tot;
int sz[MAXN];//sz[i]表示编号为 i 的其下子树的大小 
int f[MAXN],weight[MAXN];

struct Edge
{
    int from,to,cost,nxt;
}e[MAXN*2];

void addedge(int u,int v,int w)
{
    e[tot].from=u;
    e[tot].to=v;
    e[tot].cost=w;
    e[tot].nxt=head[u];
    head[u]=tot++;
}

void dfs(int u,int fa)
{
    sz[u]=1;
    for(int i=head[u];i!=-1;i=e[i].nxt)
    {
        int to=e[i].to;
        if(to==fa)  continue;
        f[to]=u;
        weight[to]=e[i].cost;
        dfs(to,u);
        sz[u]+=sz[to];
    }
}

int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(sz,0,sizeof(sz));
        memset(head,-1,sizeof(head));
        tot=0;
        int u,v,w;
        for(int i=1;i<=n-1;++i)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        dfs(1,-1);
        LL ans=0;
        for(int i=2;i<=n;i++)
        {
            ans+=(LL)weight[i]*min(sz[i],k);
        }
        printf("%lld\n",ans);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值