URL : http://acm.hdu.edu.cn/showproblem.php?pid=6060

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)res = \sum_{i = 1}^{k}{f(\{1\}\bigcup S_i)}.
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
5 4
1 2 3
2 3 4
2 4 5
2 5 6

Sample Output
27

题解

直接看代码应该就能懂了
既然要求最大值,就让边尽可能的多被使用
取1为根节点,若u -> v 有一条价值为c的边,设以节点v为根节点的子树节点个数为d(v)
那么边(u,v)最多使用min(d(v),k)min(d(v), k) 次,那么这条边的对答案的贡献就是 c⋅min(d(v),k)c\cdot min(d(v), k)

#include<stdio.h>
#include<string.h>typedef long long LL;
const int MAXN = 1e6 + 50;
struct node{int nxt, to, cost;} E[MAXN<<1];
int lnk[MAXN], tal, n, k;
LL ans;
inline void add(int x, int y, int c) {E[tal].nxt = lnk[x];E[tal].to = y;E[tal].cost = c;lnk[x] = tal++;
}
inline int min(int a, int b) {return a < b ? a : b;
}
inline int dfs(int u, int fa) {int cnt = 1;for(int i = lnk[u]; i; i = E[i].nxt) {if(fa == E[i].to) continue;int num = dfs(E[i].to, u);ans += 1LL * min(num, k) * E[i].cost;cnt += num;}return cnt;
}int main()
{while(scanf("%d%d", &n, &k) != EOF) {tal = 1;for(int i = 1; i < n; ++i) {int x, y, c;scanf("%d%d%d", &x, &y, &c);add(x, y, c);add(y, x, c);}ans = 0;dfs(1, 1);printf("%lld\n", ans);memset(lnk, 0, sizeof(int) * (n + 2));}return 0;
}

[HDU]-6060 RXD and dividing相关推荐

  1. 2017 多校3 hdu 6061 RXD and functions

    2017 多校3 hdu 6061 RXD and functions(FFT) 题意: 给一个函数\(f(x)=\sum_{i=0}^{n}c_i \cdot x^{i}\) 求\(g(x) = f ...

  2. HDU 6061 RXD and functions(NTT)

    RXD and functions 首先是有一个结论,对多项式做任意多次 transformation ,其结果跟做一次 transformation Tr(f,∑i=1mai)Tr(f, \sum\ ...

  3. 2017 ACM暑期多校联合训练 - Team 3 1008 HDU 6063 RXD and math (莫比乌斯函数)...

    题目链接 Problem Description RXD is a good mathematician. One day he wants to calculate: ∑i=1nkμ2(i)×⌊nk ...

  4. HDU 6064 RXD and numbers

    传送门 有向图生成树计数 (度数 ->入度->外向树) BEST定理 (不定起点的欧拉回路个数=某点为根的外向树个数(存在欧拉回路->每个点为根的外向树个数相等)*(每个点的度数(存 ...

  5. hdu6060 RXD and dividing

    题解:题目给出是一棵树,说明每条边都会用到,而每条边最多能用的次数为min(以该边连接的子节点为根的子树大小,k) #include<iostream> #include<stdio ...

  6. RXD and dividing

    题意:给出一棵有n个顶点的树,然后将2~n号顶点分成k块,求1号顶点到分成k块后各个顶点的最大权值和. 把1看成整棵树的根. 问题相当于把2\sim n2∼n每个点一个[1, k][1,k]的标号. ...

  7. 基尔霍夫定理 Kirchhoff's Matrix-Tree Theorem

    推荐博客: 基尔霍夫定理 直观应用: 给定一个无向图/有向图G,求它生成树的个数t(G) 可以运用高斯消元求矩阵行列式(高斯消元法有待学习) 例题: HDU 6064 RXD and numbers ...

  8. 【HDU 1059】Dividing(多重背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意:有6种物品,价值分别是1,2,3,4,5,6,每种物品有n[ i ]个,,问是否能将其平分 ...

  9. HDU 1059 Dividing

    题意:有6种大理石(marble,怀疑是不是大理石的意思...几块大理石也值得争?!),每种若干件且每种大理石有一定的价值(分别为1,2,3,4,5,6).问能否分成价值相等的两份. 分析:多重背包. ...

最新文章

  1. SecureFX连接Linux后文件夹中文乱码问题解决
  2. 基于以太坊的去中心化宠物商店构建教程
  3. postman面试_Postman 收费太贵了,我决定用 Postwoman。。。
  4. pytorch注意事项
  5. 《剑指offer》不用加减乘除做加法
  6. 使用 C# 捕获进程输出
  7. java filedialog 打开文件_java 用文件对话框打开文件
  8. Hadoop Ambari 安装
  9. ubuntu中flash中文乱码的问题解决
  10. 哲学经典名句[zt]
  11. C# 概念 委托和事件
  12. RiPlus子主题V1.3-Unreal[幻]主题WordPress主题模板美化wp主题源码
  13. word文档里四级目录无法显示
  14. 乌镇互联网大会官方首次使用AI同传,搜狗为雷军提供机器翻译
  15. C++实现JPEG格式图片解析(附代码)
  16. python公众号监控_Python-WXPY实现微信监控报警
  17. console不止console.log
  18. 提供三块Python爬虫代码学习
  19. 第十届蓝桥杯市赛Scratch编程真题解析
  20. Kafka学习笔记-常用命令

热门文章

  1. hp计算机如何进入启动界面,HP笔记本怎么进入BIOS设置启动方式,惠普怎么进入BIOS设置?...
  2. 浅析航空公司如何运用外部移动大数据
  3. STM32单片机-操作访问内部Flash
  4. Mac下Charles踩坑记录
  5. deblurGAN-v2 去模糊深度学习算法。
  6. python实现软件登陆过程中的试用时间校验(网络时间和本地时间双校验)
  7. 衡量批处理计算机系统的性能指标,操作系统概念.doc
  8. matlab车牌识别的外文文献翻译,基于MATLAB的车牌识别系统研究(课设参考文献)
  9. Tomcat--Linux下如何配置2个或多个同时运行
  10. 1.7Web前端基础笔记