Problem Description
An 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 wi 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.

Input
The 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,wi, the length of a road connecting the village i and the village j is wi.

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

Sample Output
6 3.33

题意

n个城市m条路,国王想花最少的钱使得任意两个城市连通,最后问你国王任意两个点的距离期望

题解

第一问直接跑个最小生成树

第二问树上任意两点期望,可以发现两两点总数是n*(n-1)/2种情况,然后只需要dp出任意两点距离和,再用距离和/情况数

当n=1的时候直接输出0.00,然后n*(n-1)可能会爆所以得*1LL

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. const int maxn=1e5+;
  7. const int maxm=1e6+;
  8.  
  9. int n,m;
  10. int f[maxn];
  11. vector< pair<int,ll> >G[maxn];
  12. ll dp[maxn],sum[maxn],ans;
  13. struct edge
  14. {
  15. int u,v;
  16. ll w;
  17. bool operator<(const edge& D)const{
  18. return w<D.w;
  19. }
  20. }edges[maxm];
  21. void dfs(int cur,int fa)
  22. {
  23. sum[cur]=;
  24. for(int i=;i<G[cur].size();i++)
  25. {
  26. int son=G[cur][i].first;
  27. ll w=G[cur][i].second;
  28. if(fa==son)continue;
  29. dfs(son,cur);
  30. sum[cur]+=sum[son];
  31. dp[cur]+=dp[son]+sum[son]*(n-sum[son])*w;
  32. }
  33. }
  34. int find(int x)
  35. {
  36. return f[x]==x?x:f[x]=find(f[x]);
  37. }
  38. void kruskal()
  39. {
  40. for(int i=;i<=n;i++)f[i]=i;
  41. sort(edges,edges+m);
  42. int cnt=;
  43. for(int i=;i<m;i++)
  44. {
  45. int u=edges[i].u;
  46. int v=edges[i].v;
  47. ll w=edges[i].w;
  48. int fu=find(u);
  49. int fv=find(v);
  50. if(fu!=fv)
  51. {
  52. f[fu]=fv;
  53. G[u].push_back({v,w});
  54. G[v].push_back({u,w});
  55. ans+=w;
  56. if(++cnt==n-)return;
  57. }
  58. }
  59. }
  60. int main()
  61. {
  62. int t,u,v;
  63. ll w;
  64. scanf("%d",&t);
  65. while(t--)
  66. {
  67. ans=;
  68. scanf("%d%d",&n,&m);
  69. for(int i=;i<m;i++)
  70. {
  71. scanf("%d%d%lld",&u,&v,&w);
  72. edges[i]={u,v,w};
  73. }
  74. kruskal();
  75. dfs(,);
  76. if(n==)printf("%lld 0.00\n",ans);
  77. else printf("%lld %.2f\n",ans,dp[]*1.0/(1LL*n*(n-)/));
  78. for(int i=;i<=n;i++)
  79. {
  80. dp[i]=sum[i]=;
  81. G[i].clear();
  82. }
  83. }
  84. return ;
  85. }

HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)的更多相关文章

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

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

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

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

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

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

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

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

  5. 最小生成树 kruskal hdu 5723 Abandoned country

    题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...

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

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

  7. HDU 5723 Abandoned country(落后渣国)

    HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 ...

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

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

  9. HDU 5723 Abandoned country (最小生成树 + dfs)

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

随机推荐

  1. 转"container of()函数简介"链接地址

    https://blog.csdn.net/s2603898260/article/details/79371024 提示关键字: 指针0的使用 typeof的使用

  2. 【java多线程】队列系统之说说队列Queue

    转载:http://benjaminwhx.com/2018/05/05/%E8%AF%B4%E8%AF%B4%E9%98%9F%E5%88%97Queue/ 1.简介 Queue(队列):一种特殊的 ...

  3. mac添加redis 环境变量

    cd /etc/paths.d touch redis vim redis 写入 /Users/love/Downloads/redis-4.0.10/src 之后就可以直接执行redis-cli r ...

  4. Zookeeper 集群搭建--单机伪分布式集群

    一. zk集群,主从节点,心跳机制(选举模式) 二.Zookeeper集群搭建注意点 1.配置数据文件 myid 1/2/3 对应 server.1/2/3 2.通过./zkCli.sh -serve ...

  5. Python 模块collections

    1.深入理解python中的tuple的功能 基本特性 # 可迭代 name_tuple = ('0bug', '1bug', '2bug') for name in name_tuple: prin ...

  6. verilog 除法器

    verilog 除法器:利用二进制的除法翻译过来的硬件电路 1.1 实现算法 基于减法的除法器的算法: 对于32的无符号除法,被除数a除以除数b,他们的商和余数一定不会超过32位.首先将a转换成高32 ...

  7. 减小delphi体积的方法

    1.关闭RTTI反射机制  自从Delphi2010中引入了新的RTTI反射机制后,编译出来的程序会变得很大,这是因为默认情况下 Delphi2010 给所有类都加上了反射机制.而我们的工程并不每每都 ...

  8. mysql大表设计以及优化

    MYSQL千万级数据量的优化方法积累https://m.toutiao.com/group/6583260372269007374/?iid=6583260372269007374 MySQL 千万级 ...

  9. springMVC的高级数据绑定,以及json交互,全局异常配置,

    一.窄化请求映射 1.在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理. 如下: @Con ...

  10. python3 re.compile中含有变量

    id = '7F' reg = re.compile(id + '[^\dA-F]?\d') line = ‘122s 7f 3' match = reg.search(line) 在程序中有时候会遇 ...