2017多校训练Contest3: 1005 RXD and dividing hdu6060

本文介绍了一种解决特定最小斯坦纳树问题的方法。该问题要求将一系列节点划分成若干组,并计算连接每组中所有节点的最小代价。通过为每组节点分配一个标识并利用DFS遍历树形结构,可以有效地计算出整体最小代价。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

bestcoder官方博客:

把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).

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cassert>
#include<iostream>
#include<algorithm>
using namespace std;
struct line
{
    int s,t;
    long long x;
    int next;
}a[2000001];
int head[1000001];
int edge;
inline void add(int s,int t,long long x)
{
    a[edge].next=head[s];
    head[s]=edge;
    a[edge].s=s;
    a[edge].t=t;
    a[edge].x=x;
}
bool v[1000001];
long long ans;
int m;
inline int dfs(int d)
{
    v[d]=true;
    int i;
    int sum=1;
    for(i=head[d];i!=0;i=a[i].next)
    {
        int t=a[i].t;
        if(!v[t])
        {
            int xx=dfs(t);
            sum+=xx;
            xx=min(xx,m);
            ans+=(long long)xx*a[i].x;
        }
    }
    return sum;
}
int main()
{
    int n;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        edge=0;
        memset(head,0,sizeof(head));
        int i,s,t,x;
        for(i=1;i<=n-1;i++)
        {
            scanf("%d%d%d",&s,&t,&x);
            edge++;
            add(s,t,x);
            edge++;
            add(t,s,x);
        }
        memset(v,false,sizeof(v));
        ans=0;
        dfs(1);
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值