2017 Multi-University Training Contest 3( hdu 6060) RXD and dividing
来源:互联网 发布:后三胆码计算软件 编辑:程序博客网 时间:2024/06/05 20:07
Problem Description
RXD has a tree T , with the size of n . Each edge has a cost.
Definef(S) as the the cost of the minimal Steiner Tree of the set S on tree T .
he wants to divide2,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 calulatesres=∑ki=1f({1}⋃Si) .
He wants to maximize theres .
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
he wants to divide
where
Then he calulates
He wants to maximize the
Input
There are several test cases, please keep reading until EOF.
For each test case, the first line consists of 2 integern,k , which means the number of the tree nodes , and k means the number of parts.
The nextn−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 meansn≤100 .
For each test case, the first line consists of 2 integer
The next
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
Output
For each test case, output an integer, which means the answer.
Sample Input
5 41 2 32 3 42 4 52 5 6
Sample Output
27
Source
2017 Multi-University Training Contest - Team 3
题意:把2~n这n-1个点分成k份,然后定义一个f(s)表示要把集合S里点全部变成联通要加边权的和的最小值。
然后问你怎么分这n-1个点才能使每个集合加上只有点1这个集合的f(s)的和最大,求出这个最大值。集合可以为空集。
可以发现,空集是没有什么必要的,如果合并两个集合,必然会使答案变小或者不变,所以我们尽量让点分布到不同的集合中去。
我们可以考虑每一条边的贡献,对于一个点,它与它父亲连的那条边,只有可能会被这个点,还有这个点的后代经过,所以我们想让这个点与它的后代尽可能的分到不同的集合中去,所以这条边的贡献就是min(k,这个点后代的个数加上它本身这个点)。
#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define LL long longusing namespace std;struct edge{ int to,cost; edge(int _to,int _cost) { to = _to; cost = _cost; }};vector<edge> e[1000010];int n,k;LL ans;int dfs(int x,int pre,int PreEdge){ int sz = 1; for(int i=0;i<e[x].size();i++) { int xx = e[x][i].to; if(xx == pre) continue; sz += dfs(xx,x,e[x][i].cost); } ans += (LL)PreEdge*min(k,sz); return sz;}int main(void){ int T,i,j; while(scanf("%d%d",&n,&k)==2) { for(i=1;i<=n;i++) e[i].clear(); for(i=1;i<=n-1;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); e[x].push_back(edge(y,z)); e[y].push_back(edge(x,z)); } ans = 0; dfs(1,-1,0); printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- 2017 Multi-University Training Contest 3( hdu 6060) RXD and dividing
- hdu 6060 RXD and dividing(树)(2017 Multi-University Training Contest
- 2017 Multi-University Training Contest 3 solutions 1008 RXD and math
- HDU 6065 RXD, tree and sequence (LCA, 2017 Multi-Univ Training Contest 3)
- 2017 Multi-University Training Contest 3 solutions 1011 RXD's date
- HDU 3068 2017 Multi-University Training Contest
- HDU 6034 & 2017 Multi-University Training Contest
- hdu 6034 2017 Multi-University Training Contest
- HDU 3065 2017 Multi-University Training Contest
- HDU 6047 2017 Multi-University Training Contest
- HDU 6052 2017 Multi-University Training Contest
- HDU 6058 2017 Multi-University Training Contest
- HDU 6078 2017 Multi-University Training Contest
- (hdu 6078)2017 Multi-University Training Contest
- hdoj 6060(2017 Multi-University Training Contest
- 2017 Multi-University Training Contest 3
- 2017 Multi-University Training Contest 3 1005
- hdu 6060 RXD and dividing (贪心)
- 2.1.16 —线性表—Rotate Image
- 【几何】hdu 6055 Regular polygon
- android 启动时出现空白带标题的页面解决办法
- 关于APP广告也面图片的保存问题
- 百练noi21:二维数组右上左下遍历,24:蛇形填充数组
- 2017 Multi-University Training Contest 3( hdu 6060) RXD and dividing
- spring-batch示例配置文件
- Android 5.0 Camera系统源码分析(4):Camera预览流程数据流
- ARKit从入门到精通(2)-ARKit工作原理及流程介绍
- 实现ListView头部图片拉升方大松手回弹
- List的子类ArrayList .Vector , LinkedList的特点
- 中文字符串长度问题:strlen和mb_strlen
- 一步步实现:JPA的基本增删改查CRUD(jpa基于hibernate)
- npm 的rollbackFailedOptional以及node.js npm fetchmetadata 卡住错误解决~