HDU 6060-RXD and dividing(多校训练第三场->DFS)

RXD and dividing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 368    Accepted Submission(s): 154


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 SiSj= .
Then he calulates res=ki=1f({1}Si) .
He wants to maximize the res .
1kn106
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 n1 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 n100 .
 

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
 

Recommend

题意:给你一棵树,让你将2~n这n-1个点分成k个集合,并使得1在任何一个集合里,定义f(s)为使得集合s中所有点联通的最小花费(花费为边的权值),当然,根据斯坦纳树的定义,你可以往上加边使得所有点都联通,然后问你这k个集合所花总成本的最大值。
题解:把1看做根,原问题其实可以转化为在这棵树上给2~n这n-1个点标号,编号的范围是[1,k],根据最小斯坦纳树的定义,(x,fa[x])这条边的贡献是x子树内不同标号的个数num,x显然num<=min(k,size[x]);其中k为编号的个数,size[x]表示x子树的大小,根据贪心思想,你要使得每个边的贡献最大,也就是让num最大,呢这一条边的答案就是
min(k,size[x])*w;   w为x到它父亲的边权。。


#include<map>      
#include<stack>      
#include<queue>    
#include<vector>      
#include<math.h>      
#include<stdio.h>    
#include<iostream>  
#include<string.h>      
#include<stdlib.h>      
#include<algorithm>      
using namespace std;      
typedef long long  ll;      
#define inf 1000000000      
#define mod 1000000007     
#define maxn  5000905  
#define lowbit(x) (x&-x)      
#define eps 1e-10    
ll a[maxn],ans=0,n,k;
struct node
{
	ll v,w;
};
vector<node>q[maxn];
ll dfs(ll u,ll p,ll c)
{
	a[u]=1;
	int i;
	for(i=0;i<q[u].size();i++)
	{
		node v=q[u][i];
		if(v.v==p)
			continue;
		a[u]+=dfs(v.v,u,v.w);
	}
	ans+=min(k,a[u])*c;
	return a[u];
}
int  main(void)
{
	node tmp;
	ll i,j,x,y,z;
	while(scanf("%lld%lld",&n,&k)!=EOF)
	{
		ans=0;
		for(i=1;i<n;i++)
		{
			scanf("%lld%lld%lld",&x,&y,&z);
			tmp.v=y;tmp.w=z;
			q[x].push_back(tmp);
		}
		dfs(1,0,0);
		printf("%lld\n",ans);
		for(i=1;i<=n;i++)
			q[i].clear();
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值