hdu 6060 RXD and dividing (贪心)

RXD has a tree  T T, with the size of  n n. Each edge has a cost. 
Define  f(S) f(S) as the the cost of the minimal Steiner Tree of the set  S S on tree  T T
he wants to divide  2,3,4,5,6,n 2,3,4,5,6,…n into  k k parts  S1,S2,S3,Sk S1,S2,S3,…Sk
where  Si={2,3,,n} ⋃Si={2,3,…,n} and for all different  i,j i,j , we can conclude that  SiSj= Si⋂Sj=∅
Then he calulates  res=ki=1f({1}Si) res=∑i=1kf({1}⋃Si)
He wants to maximize the  res res
1kn106 1≤k≤n≤106 
the cost of each edge[1,105] the cost of each edge∈[1,105] 
Si Si might be empty. 
f(S) f(S) means that you need to choose a couple of edges on the tree to make all the points in  S S connected, and you need to minimize the sum of the cost of these edges.  f(S) 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 n,k, which means the number of the tree nodes , and  k k means the number of parts. 
The next  n1 n−1 lines consists of 2 integers,  a,b,c a,b,c, means a tree edge  (a,b) (a,b) with cost  c 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 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
 
 
给你一个树n个数,分成m组,每组的最小生成树权值加起来的总和最小
其实是个水题。。队友想复杂了。。(都想到拓扑了)。最后自己出坑发现正确思路还是很开心的
(ε=ε=ε=┏(゜ロ゜;)┛逃  算每个点能用的次数 和k比较 取小的,就是能用的点尽可能贡献
 
 
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e6+100;
int sze[N];
typedef pair<int,ll> P;
vector<P> e[N];
	int n,m;
	ll ans=0;
void dfs(int x,int y,ll w)
{
	sze[x]=1;
	for(int i=0;i<e[x].size();i++)
	{
		if(e[x][i].first==y) continue;
		dfs(e[x][i].first,x,e[x][i].second);
		sze[x]+=sze[e[x][i].first];
	}
	ans+=1LL*min(sze[x],m)*w;
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		ans=0;
		for(int i=1;i<=n;i++)
			e[i].clear();
		for(int i=1;i<n;i++)
		{
			int a,b;ll c;
			scanf("%d%d%lld",&a,&b,&c);
			e[a].push_back(P(b,c));
			e[b].push_back(P(a,c));
		}
		dfs(1,0,0);
		printf("%lld\n",ans );
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值