Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3449    Accepted Submission(s):
846

Problem DescriptionAn abandoned country has n(n≤100000)villages which are numbered from 1 to n . Since abandoned for a long time, the roads need to be re-built. There are
m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000) . Guaranteed that any two w are different. The roads made all the villages connected directly or indirectly
before destroyed. Every road will cost the same value of its length to rebuild.
The king wants to use the minimum cost to make all the villages connected with
each other directly or indirectly. After the roads are re-built, the king asks a
men as messenger. The king will select any two different points as starting
point or the destination with the same probability. Now the king asks you to
tell him the minimum cost and the minimum expectations length the messenger will
walk.
 
InputThe first line contains an integer T(T≤10) which indicates the number of test cases. 

For each test case, the first
line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next
m lines, each line have three number i,j,w , the length of a road connecting the village i and the village j is w .
 
Output
output the minimum cost and minimum Expectations with
two decimal places. They separated by a space.
 
Sample Input
1
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
 

题意:求最小生成树,再求任意两点距离的期望。

最小生成树用kruscal。期望一开始不会求,看题解发现使用dfs:分别求每条边的贡献,一条边的贡献等于这条边的|左子树|*|右子树|*value。

代码中使用pair和vector

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<vector>
  6. using namespace std;
  7.  
  8. struct Path
  9. {
  10. int x,y;
  11. double value;
  12. } path[];
  13.  
  14. typedef pair<int ,int> P;
  15. vector <P> edge[];
  16.  
  17. bool cmp(Path a,Path b)
  18. {
  19. return a.value<b.value;
  20. }
  21. int father[],n,m;;
  22.  
  23. int find(int x)
  24. {
  25. if(x!=father[x])
  26. father[x]=find(father[x]);
  27. return father[x];
  28. }
  29.  
  30. void merge(int a,int b)
  31. {
  32. int x=find(a);
  33. int y=find(b);
  34. if(x!=y)
  35. father[x]=y;
  36. }
  37.  
  38. long long sumv=,sumd=;
  39. void kruscal()
  40. {
  41. for(int i=; i<=n; i++)
  42. father[i]=i;
  43. for(int i=; i<=n; i++)
  44. edge[i].clear();
  45. sort(path,path+m,cmp);
  46. for(int i=; i<m; i++)
  47. {
  48. int x=path[i].x,y=path[i].y,value=path[i].value;
  49. if(find(path[i].x)!=find(path[i].y))
  50. {
  51. sumv+=path[i].value;
  52. merge(path[i].x,path[i].y);
  53. edge[x].push_back(P(y,value));
  54. edge[y].push_back(P(x,value));
  55. }
  56. }
  57. }
  58.  
  59. int dfs(int x,int last)
  60. {
  61. int cnt=;
  62. for(int i=;i<edge[x].size();i++)
  63. {
  64. int y=edge[x][i].first,value=edge[x][i].second;
  65. if(last!=y)
  66. {
  67. int now=dfs(y,x);
  68. cnt+=now;
  69. sumd+=1.0*now*(n-now)*value;
  70. }
  71. }
  72. return cnt+;
  73. }
  74. int main()
  75. {
  76. int t;
  77. scanf("%d",&t);
  78. while(t--)
  79. {
  80. sumv=;
  81. sumd=;
  82. scanf("%d%d",&n,&m);
  83. for(int i=; i<m; i++)
  84. {
  85. scanf("%d%d%lf",&path[i].x,&path[i].y,&path[i].value);
  86. }
  87. kruscal();
  88. dfs(,-);
  89. printf("%I64d %.2lf\n",sumv,sumd*2.0/(1.0*n)/(n-1.0));
  90. }
  91. return ;
  92. }
Sample Output
6 3.33
 
Author
HIT
 
Source

HDU_5723_最小生成树+任意两点距离的期望的更多相关文章

  1. HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)

    Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...

  2. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  3. HDU 2376 树形dp|树上任意两点距离和的平均值

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...

  4. HDU2376Average distance(树形dp|树上任意两点距离和的平均值)

    思路: 引:如果暴力枚举两点再求距离是显然会超时的.转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的 ...

  5. 【非原创】codeforces 1060E Sergey and Subway 【树上任意两点距离和】

    学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 con ...

  6. HDU 5723 Abandoned country 【最小生成树&&树上两点期望】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...

  7. JAVA 计算地球上任意两点(经纬度)距离

    /** * 计算地球上任意两点(经纬度)距离 * * @param long1 * 第一点经度 * @param lat1 * 第一点纬度 * @param long2 * 第二点经度 * @para ...

  8. caioj 1237: 【最近公共祖先】树上任意两点的距离 在线倍增ST

    caioj 1237: [最近公共祖先]树上任意两点的距离 倍增ST 题目链接:http://caioj.cn/problem.php?id=1237 思路: 针对询问次数多的时候,采取倍增求取LCA ...

  9. php根据地球上任意两点的经纬度计算两点间的距离 原理

    地球是一个近乎标准的椭球体,它的赤道半径为6378.140千米,极半径为6356.755千米,平均半径6371.004千米.如果我们假设地球是一个完美的球体,那么它的半径就是地球的平均半径,记为R.如 ...

随机推荐

  1. Linux下C++访问MySQL数据库

    由于想要开始了解并学习用LAMP进行web开发,所以昨晚我在Fedora上安装了MySQL,学习了MySQL的几个常用命令.想着在学习进行web开发(PHP访问数据库)之前,先用我熟悉的C++连接数据 ...

  2. leetcode第一刷_Spiral Matrix

    我认为这个题好无聊啊,好端端一个数组.干嘛要跟比巴卜一样转一圈输出呢. . 思想非常easy,每次从左到右.再从上到下,在从右到左,再从下到上.问题是每次到什么时候该改变方向.我的做法是用一个变量保存 ...

  3. java-javabean Introspector的应用

    Introspector 类为通过工具学习有关受目标 Java Bean 支持的属性.事件和方法的知识提供了一个标准方法. 对于这三种信息,Introspector 将分别分析 bean 的类和超类, ...

  4. bootstrap table load数据

    直接load数据: $("#button").click(function(){ var name=$("input[name='name']").val(); ...

  5. Linux设备驱动模型【转】

    本文转载自:http://blog.csdn.net/xiahouzuoxin/article/details/8943863 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+ ...

  6. bzoj1805: [Ioi2007]Sail 船帆

    可以发现旗杆的顺序是没有用的,对于每列,它的答案是它的最大值mx*(mx+1)/2 高度由小到大排序旗杆,问题可以转化为在前h行选k个最小的值 考虑激情splay乱搞(我只会splay......) ...

  7. Jsp内置对象和EL隐藏(内置)对象

      JSP中的内置对象一共有九个, 由于有的不太常用, 所以总是记不住, 从Sun公司的网站上找到的PDF文档, 把这一部分放在这里, 以备随时查用:     JSP九个内置对象: Implicit ...

  8. 怎么让frameset出现整体滚动条

    a.html<!DOCTYPE html> <html> <head> <title></title> </head> < ...

  9. Django - CBV装饰器实现用户登录验证

    一.使用Django自带的decorator 通常情况,使用 函数定义的view,可以直接使用 login_required 直接装饰 @login_required def index(reques ...

  10. [Swift通天遁地]七、数据与安全-(16)检测Apple设备是否越狱

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...