地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6060

题目:

RXD and dividing

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

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
5 4
1 2 3
2 3 4
2 4 5
2 5 6
 
Sample Output
27
 
Source
 思路:
  智商感觉被碾压。题解是从整体考虑的,而我一直从局部考虑,试图求出各个集合。。。走远了
  

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; vector<PII>mp[K];
int n,k,sz[K];
LL ans;
void dfs(int u,int f)
{
sz[u]=;
for(auto v:mp[u])if(f!=v.first) dfs(v.first,u),sz[u]+=sz[v.first];
for(auto v:mp[u])if(f!=v.first) ans+=1LL*v.second*min(sz[v.first],k);
}
int main(void)
{ while(~scanf("%d%d",&n,&k))
{
ans=;
memset(mp,,sizeof mp);
for(int i=,x,y,z;i<n;i++)
scanf("%d%d%d",&x,&y,&z),mp[x].PB(MP(y,z)),mp[y].PB(MP(x,z));
dfs(,);
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. leetcode--Maximum Subarray
  2. SpringSecurity操作指南-基于Spring、SpringMVC和MyBatis自定义SpringSecurity权限认证规则
  3. odoo注销后在登录时的用户名和密码
  4. jquery之 off()方法
  5. es6转码器-babel
  6. C#不安全代码
  7. android92 aidl远程进程通信
  8. MySQL5.6 windows7下安装及基本操作
  9. Run QTP script wiht host in HPQC
  10. flask 扩展之 -- flask-login
  11. 通过tokenPlease()函数获取accessToken
  12. 流程帮App风险评估
  13. @ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常==》记录
  14. Codeforces551 C. GukiZ hates Boxes
  15. Python老男孩
  16. python全栈开发day12
  17. [Robot Framework] 执行时报 webdriver 异常
  18. javascript:原型与原型链
  19. AGC 007 D - Shik and Game
  20. Dockerfile基本语法

热门文章

  1. VS2008设置快捷键Ctrl+W关闭当前打开的文本编辑器窗口
  2. ETL概念,ETL流程
  3. Using XSLT and Open XML to Create a Word 2007 Document
  4. iOS开发之 -- 获取设备的唯一标示符
  5. 有Thread1、Thread2、Thread3、Thread4四条线程分别统计C、D、E、F四个盘的大小,所有线程都统计完毕交给Thread5线程去做汇总,应当如何实现?
  6. python--常用的十进制、16进制、字符串、字节串之间的转换
  7. 引入 netty网关,向flume提交数据
  8. mysql一个特殊的条件.字符串除以0的结果.
  9. 剑指Offer——左旋转字符串
  10. python爬虫系列(2)—— requests和BeautifulSoup