您的位置:首页 > 其它

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

2017-08-02 10:06 387 查看

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 Si⋂Sj=∅. 

Then he calulates res=∑ki=1f({1}⋃Si).

He wants to maximize the res.
1≤k≤n≤106
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 n−1 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 n≤100.
 

Output

For each test case, output an integer, which means the answer.
 

Sample Input

5 4
1 2 3
2 3 42 4 5
2 5 6

 

Sample Output

27

 

Source

2017 Multi-University Training Contest - Team 3
 
———————————————————————————————————
题意:给一个n个节点的树,要求将2-n号节点分成k部分,然后将每一部分加上节点1,
每一个子树的val为最小斯坦纳树,求总的最大val
其实这里的斯坦纳树很简单,就是在原树中找最少的边是的一个集合里的点全部连通
思路:比赛时在那贪心构造搞,搞了半天弄不出,赛后才发现很简单的
先贴一下官方题解:
把1看成整棵树的根. 问题相当于把2\sim
n2∼n每个点一个[1,
k][1,k]的标号.
然后根据最小斯坦纳树的定义, (x,
fa_x)(x,fa​x​​) 这条边的
贡献是 x 子树内不同标号的个数目dif_idif​i​​.
那么显然有dif_i\leq
min(k, sz_i)dif​i​​≤min(k,sz​i​​), sz_isz​i​​表示子树大小.
可以通过构造让所有dif_idif​i​​都
取到最大值. 所以答案就是
\sum_{x
= 2}^{n}{w[x][fa_x] * min(sz_x, k)}∑​x=2​n​​w[x][fa​x​​]∗min(sz​x​​,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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: