您的位置:首页 > 其它

杭电暑期多校集训—RXD and dividing

2017-08-06 12:24 435 查看


RXD and dividing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Total Submission(s): 1353    Accepted Submission(s): 244


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

 

Sample Output

27
题目描述
给一棵树T,有n个结点。
给一个k,表示有k个集合,我们需要把2,3,4,…n号节点放入集合,要保证k个集合的并集等于{2,3,4,5n},并且集合互不相交。(集合可以为空)然后每次取一个集合Si与{1}求并,得到比如{1,2,3},那么tempi = f({1,2,3});f({1}并Si)的意思是把合内的所有点连接起来的边的权值和。最后把所有权值和相加的到答案。解题思路
我们要想得到最大的答案,那么就要尽可能的去利用这些边,也就是尽可能重复计算这些边。
那么我们想,假设先从叶子节点开始,把这些叶子节点放入一个集合,那么这个集合的temp值就会把所有的边都算一遍。那么下次我们取所有叶子节点的父亲,放入一个集合,那么这个集合的temp值会把除了叶子节点到父亲的那条那边的其他所有边都算一遍。因为集合可以为空,以此类推,我们就可以得到最大的答案。但是如果遇到集合不够的情况,就把剩下的所有点加入最后一个集合。
那么有以上分析,其实就是算每条边会算多少次,比如叶子节点到父亲的那条边会算一次。其实一条边会算多少次跟某个点的所有子孙节点个数有关,就比如样例中,2号点有3个子孙节点, 那么2号点连接父节点的那条边会算3+1次。3号点有0个子孙节点,那么3号点连接父节点的那条边会算0+1次。
那么其实问题就是转化为求每个点的子孙节点个数,然后算出每条边要重复计算的次数即可。
 
#include<bits/stdc++.h>
using namespace std;

struct Edge{
int v,w;
};

Edge temp;
vector<Edge> vec[1000005];
int Size[1000005];  ///保存每个节点的子节点的个数
int w[1000005];  ///存权重

void dfs(int u,int pre){  ///递归找到每一个节点的子节点的个数
Size[u]=1;
int len=vec[u].size();
for(int i=0;i<len;i++){
int v=vec[u][i].v;
if(v!=pre){
w[v]=vec[u][i].w;
dfs(v,u);
Size[u]+=Size[v];
}
}
}

int main(){
int n,k;
while(~scanf("%d%d",&n,&k)){
for(int i=1;i<=n;i++){
vec[i].clear();
Size[i]=0;
w[i]=0;
}
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++){
sum+=(long long)w[i]*min(Size[i],k);  ///k个集合,字节个数要和k比较取小的
}
printf("%lld\n",sum);
}
return 0;
}


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