RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1541 Accepted Submission(s): 664
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
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 .
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
Source
题意:给一个1到n的序列,除1外,将它们分成k个不相交的集合,现在每个集合加入1,求所有集合最小斯坦纳树和的最大值;
解题思路:若要使权值和最大,考虑每一条边的贡献次数,如果从父亲向下的一条边,子树大小小于k,设为x,下面至多被分为x个分块,该边被贡献x次,否则至多分成k个分块,该边贡献k次,所以每条边最大情况下应该被计算min(x,k)次,x为该边下方结点的子树大小
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n, k, u, v;
int s[1000009], nt[2000009], e[2000009], sum[1000009];
LL val[2000009], w, ans;
void dfs(int x, int fa)
{
sum[x] = 1;
for (int i = s[x]; ~i; i = nt[i])
{
if (e[i] == fa) continue;
dfs(e[i], x);
sum[x] += sum[e[i]];
ans += 1LL*min(sum[e[i]], k)* val[i];
}
}
int main()
{
while (~scanf("%d%d", &n, &k))
{
memset(s, -1, sizeof s);
int cnt = 0;
for (int i = 1; i < n; i++)
{
scanf("%d%d%lld", &u, &v, &w);
nt[cnt] = s[u], s[u] = cnt, e[cnt] = v, val[cnt++] = w;
nt[cnt] = s[v], s[v] = cnt, e[cnt] = u, val[cnt++] = w;
}
ans = 0;
dfs(1, 0);
printf("%lld\n", ans);
}
return 0;
}