RXD and dividingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1344 Accepted Submission(s): 569
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
Sample Output
Source
|
-
对于每一个子树,将子树中的每一个节点分到不同的集合的时候,所需要的边权是最大的,所以考虑每一课子树它的节点数和k的大小,这个值就是这个子树连接它父亲的边的经过的总次数。
-
问题的实质就是转化为求每个点的子孙节点个数,然后算出每条边要重复计算的次数即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define maxn 510
const int M=1e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-10;
ll n,m,k;
struct node
{
int x,val;
};
vector<node>v[M];
ll d[M],sz[M];
void dfs(int u,int fa)
{
sz[u]=1;
for(int i=0;i<v[u].size();i++){
node cur=v[u][i];
if(cur.x==fa)continue;
d[cur.x]=cur.val;
dfs(cur.x,u);
sz[u]+=sz[cur.x];
}
}
int main()
{
while(~scanf("%lld%lld",&n,&k)){
for(int i=0;i<n-1;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
v[a].push_back(node{b,c});
v[b].push_back(node{a,c});
}
dfs(1,-1);
ll ans=0;
for(int i=2;i<=n;i++)
ans+=d[i]*min(k,sz[i]);
printf("%lld\n",ans);
for(int i=1;i<=n;i++)v[i].clear();
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define maxn 510
const int M=1e7+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-10;
int n,m,k;
struct Edge
{
int v,nxt,w;
}edge[M*2];
int tot,head[M],sz[M];
ll ans;
void add(int u,int v,int w)
{
edge[tot]=(Edge){v,head[u],w};head[u]=tot++;
edge[tot]=(Edge){u,head[v],w};head[v]=tot++;
}
void dfs(int u,int fa)
{
sz[u]=1;
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].v,w=edge[i].w;
if(v==fa)continue;
dfs(v,u);
sz[u]+=sz[v];
ll tol=sz[v]<k?sz[v]:k;
ans+=tol*w;
}
}
int main()
{
while(~scanf("%d%d",&n,&k)){
ms(head,-1);
tot=ans=0;
for(int i=1;i<n;i++){
int v,u,c;
scanf("%d%d%d",&u,&v,&c);
add(u,v,c);
}
dfs(1,-1);
printf("%lld\n",ans);
}
return 0;
}
|