RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 829 Accepted Submission(s): 347
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=
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 3 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
Source
2017 Multi-University Training Contest - Team 3
题意:
将编号为2~n的点放入k个集合中,求k个树边权和的和的最大值。
样例解释:
集合1{1,2},集合2{1,3},集合3(1,4),集合4{1,5}。
ans=w[1][2]+w[1][3]+w[1][4]+w[1][5]=3+7+8+9=27
思路:
我们可以这样想,一个边的贡献为min(k,num[i])*cost,num[i]是以i为根的子树点的个数,cost为父亲节点到该点的边权,因为这条边最多可多使用min(k,num[i])次。
示例程序:
/*
Problem : 6060 ( RXD and dividing ) Judge Status : Accepted
RunId : 21451579 Language : G++ Code Len : 992 B
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct jj
{
int v,next,c;
}w[1000000];
int h[1000000],numw,num[1000000];
long long cost[1000000];
void insert(int u,int v,int c)
{
w[numw].v=v;
w[numw].c=c;
w[numw].next=h[u];
h[u]=numw++;
}
void dfs(int pos)
{
int i;
num[pos]=1;
for(i=h[pos];i!=-1;i=w[i].next)
{
cost[w[i].v]=w[i].c;
dfs(w[i].v);
num[pos]=num[pos]+num[w[i].v];
}
}
int main()
{
int n,k,i,u,v,c;
long long sum;
while(scanf("%d %d",&n,&k)!=EOF)
{
memset(h,-1,sizeof(h));
memset(num,0,sizeof(num));
numw=0;
sum=0;
for(i=1;n>i;i++)
{
scanf("%d %d %d",&u,&v,&c);
insert(u-1,v-1,c);
}
dfs(0);
for(i=1;n>i;i++)
{
sum=sum+cost[i]*min(k,num[i]);
}
printf("%lld\n",sum);
}
return 0;
}