1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node{
  5. int u, v, w, nex;
  6. bool gone;
  7.  
  8. node(){}
  9. node(int a,int b,int c){
  10. u = a;v = b;w = c;gone = false;
  11. }
  12. bool operator <(const node&a)const{
  13. return w < a.w;
  14. }
  15. };
  16. const int maxE = ;
  17. const int maxP = ;
  18. node Gra[maxE];
  19. int dp[maxP], F[maxP];
  20. int T,n,m;
  21. long long ans;
  22. double edgeSum;
  23. vector<pair<int, int> >mp[maxP];
  24.  
  25. int Find(int x){
  26. if(F[x] == x) return x;
  27. return F[x] = Find(F[x]);
  28. }
  29.  
  30. void init(int nn){
  31. for(int i = ; i <= nn; i ++){
  32. F[i] = i;
  33. mp[i].clear();
  34. }
  35. edgeSum = ;
  36. ans = ;
  37. }
  38.  
  39. int dfs(int rt, int no){
  40. int point = ;
  41. for(int i = mp[rt].size() - ; i >=; i --){
  42. int to = mp[rt][i].first;
  43. int w = mp[rt][i].second;
  44. if(to == no) continue;
  45. point += dfs(to, rt);
  46. double temp = ((double)dp[to] * (double)(n - dp[to]) * w);
  47. edgeSum += temp;
  48. }
  49. dp[rt] = point;
  50. return point;
  51. }
  52.  
  53. int main(){
  54. int w,v,u;scanf("%d",&T);
  55. while(T --){
  56. scanf("%d%d",&n,&m);
  57. init(n);
  58. for(int i = ; i < m; i ++){
  59. scanf("%d%d%d",&Gra[i].u,&Gra[i].v,&Gra[i].w);
  60. }
  61. sort(Gra, Gra + m);
  62. for(int i = ; i < m; i ++){
  63. u = Gra[i].u;v = Gra[i].v;w = Gra[i].w;
  64. int fina = Find(u), finb = Find(v);
  65. if(fina != finb){
  66. F[fina] = finb;
  67. ans = ans + w;
  68. mp[u].push_back(make_pair(v,w));
  69. mp[v].push_back(make_pair(u,w));
  70. }
  71. }
  72. dfs(, );
  73. printf("%lld %.2lf\n",ans,edgeSum*2.0/n/(n - ));
  74. }
  75. return ;
  76. }

Abandoned country(最小生成树+树形DP)的更多相关文章

  1. HDU 5723 Abandoned country 最小生成树+搜索

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...

  3. hdu 4081 最小生成树+树形dp

    思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...

  4. Install Air Conditioning HDU - 4756(最小生成树+树形dp)

    Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...

  5. hdu 5723 Abandoned country 最小生成树 期望

    Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  6. hdu 5723 Abandoned country 最小生成树+子节点统计

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  7. 【HDU - 4340】Capturing a country(树形DP)

    BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...

  8. HDU 5723 Abandoned country(最小生成树 + 树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5723 [题目大意] n座城市,m条路径,求解: 1.最短的路径和,使得n座城市之间直接或者间接连通 ...

  9. hdu4126Genghis Khan the Conqueror (最小生成树+树形dp)

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others) Total Submiss ...

随机推荐

  1. MonkeyRunner_运行脚本(一)

    前提:环境已部署, 使用数据线连接上真机  一.使用cmd窗口单步执行monkeyrunner命令 打开cmd窗口,输入monkeyrunner (前提设置好环境变量):然后按照monkeyrunne ...

  2. elastic search范围查询

    queryBuilder.must(QueryBuilders.rangeQuery("pt_longitude").from(minLongitude).to(maxLongit ...

  3. 遍历出文档内所有元素的tagName

    //深度优先 function deepLogTagNames(parentNode){ console.log(parentNode.tagName); const childNodes=paren ...

  4. tomcat安装apr优化

    APR是apache的一个linux操作系统级优化库,可以在tomcat中使用操作系统级native调用大大提高并发处理效率 先安装前置依赖: yum install -y apr-devel ope ...

  5. hotplug 热拔插机制框架

    框架入口源文件: mdev.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6     硬件平台:JZ2440 以下是驱动框架:

  6. LeetCode-714.Best Time to Buy and Sell Stock with Transaction Fee

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  7. shell日志分析进阶篇

    前面我们说了shell分析日志常用指令,现在我们随ytkah一起看看shell日志分析进阶篇,假设日志文件为ytkah.log //统计不重复抓取数量 cat ytkah.log | awk '{pr ...

  8. ros 运行rviz时出现 QXcbConnection: XCB error: 148 错误 解决方法

    出现上述问题的原因: 1.由于使用了nvc远程控制下位机: 2.rviz是一个基于opengl开发的图形插件,需要使用理论的屏幕参数(thetis' screen),由于使用了teamviewer会导 ...

  9. MSSQL查询收缩和备份进度

    --查询当前数据库备份进度 SELECT   DB_NAME(er.[database_id]) [DatabaseName],er.[command] AS [CommandType],er.[pe ...

  10. SQL存储过程分页

    CREATE PROC ZDY_FY(@Pages INT, @pageRow INT) --@Pages第几页 @pageRow每页显示几行 AS BEGIN DECLARE @starNum IN ...