HDU 5723 Abandoned country落后渣国

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

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.

一个落后渣国有n(n≤100000)座从1到n编号的村子。荒废久了,路也该重新修修。有m(m≤1000000)条路需要重建,每条路长wi(wi≤1000000)。每条路长wi各不相同。旧时的路使村子直接或间接连通。每条路重修的单位造价相同。国王希望花最少的钱使各个村子直接或间接连通。待到这些路重修好后,国王会物色一个信使。国王会等概率地选择两个点作为起点和终点。现在国王想问你这个工程的最低造价与信使所走过路程的期望。

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.

第一行是一个整数T(T≤10)表示测试用例的数量。

对于每个测试用例,第一行有两个整数n,m表示村子与待重修路的数量。接下来m行,每行有三个数i,j,wi,表示有一条长wi的路连接村子i与j。

Output

输出

output the minimum cost and minimum Expectations with two decimal places. They separated by a space.

输出最低造价与保留两位小数的最小期望。用一个空格分开。

Sample Input - 输入样例

Sample Output - 输出样例

1
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6

6 3.33

【题解】

  最小生成树+求期望,注意越界。

    期望 = 总路程/方法数。

    总路程分散到每条路上求,每条路经过的距离=长度*上方节点数*下方节点数。DFS可轻松求下方节点数,因此,上方节点数=n-下方节点数。

    方法数=1+2+……+(n-1),即n*(n-1)/2。

【代码 C++】

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. int n, m, link[], head[], iE;
  5. __int64 fCost, sum, cnt;
  6. struct Edge{
  7. int to, next, len;
  8. }edge[];
  9. void addEdge(int u, int v, int len){
  10. edge[iE].to = v; edge[iE].len = len; edge[iE].next = head[u];
  11. head[u] = iE++;
  12. }
  13. struct Road{
  14. int a, b, len;
  15. bool operator<(const Road &r) const{
  16. return len < r.len;
  17. }
  18. }iRoad[];
  19. int fid(int a){
  20. return link[a] == a ? a : link[a] = fid(link[a]);
  21. }
  22. bool beCnct(int a, int b){
  23. if (a == b) return ;
  24. return link[a] = b;
  25. }
  26. void build(){
  27. int i, j, u, v, len;
  28. for (i = ; i <= n; ++i) link[i] = i;
  29. fCost = iE = ; memset(head, -, sizeof(head));
  30.  
  31. for (i = ; i < m; ++i) scanf("%d%d%d", &iRoad[i].a, &iRoad[i].b, &iRoad[i].len);
  32. std::sort(iRoad, iRoad + m);
  33.  
  34. for (i = , j = ; j < n; ++i){
  35. u = iRoad[i].a; v = iRoad[i].b; len = iRoad[i].len;
  36. if (beCnct(fid(u), fid(v))){
  37. addEdge(u, v, len); addEdge(v, u, len);
  38. fCost += len; ++j;
  39. }
  40. }
  41. cnt = (__int64)n*(n - ) >> ;
  42. }
  43.  
  44. __int64 DFS(int now, int pre){
  45. int i;
  46. __int64 opt = , temp;
  47. for (i = head[now]; ~i; i = edge[i].next){
  48. if (i == pre) continue;
  49. opt += temp = DFS(edge[i].to, i ^ );
  50. sum += temp*(n - temp)*edge[i].len;
  51. }
  52. return opt;
  53. }
  54. int main(){
  55. int t;
  56. scanf("%d", &t);
  57. while (t--){
  58. scanf("%d%d", &n, &m);
  59. build();
  60. sum = ; DFS(, -);
  61. printf("%I64d %.2lf\n", fCost, 1.0*sum / cnt);
  62. }
  63. return ;
  64. }

HDU 5723 Abandoned country(落后渣国)的更多相关文章

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

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

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

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

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

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

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

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

  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(kruskal+dp树上任意两点距离和)

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

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

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

  8. hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...

随机推荐

  1. COM编程之二 接口

    [1]接口 DLL的接口是它所输出的那些函数. C++类的接口是该类的一个成员函数集. COM接口是包含一个函数指针数组的内存结构. 每一个数组元素包含的是一个由组件所实现的函数的地址. 在COM中接 ...

  2. svn使用相关问题:eclipse插件,加锁,解锁,偷锁,更新不了,记住密码

    svn使用相关问题:eclipse插件,加锁,解锁,偷锁,更新不了,记住密码 获取锁的时候可以看下 是谁锁住了,让对方提交解锁,如果是给离职人员锁住需要使用偷锁的方式先解锁再提交偷锁处理办法:选中该文 ...

  3. C# HttpClient, 使用C#操作Web

    我们知道, .Net类库里提供了HttpWebRequest等类,方便我们编程与Web服务器进行交互. 但是实际使用中我们经常会遇到以下需求,基础类里没有直接提供相应的功能(WebClient类包含这 ...

  4. require()与 require_once()、 include与include_once()

  5. Mysql ibdata 丢失或损坏如何通过frm&ibd 恢复数据

    mysql存储在磁盘中,各种天灾人祸都会导致数据丢失.大公司的时候我们常常需要做好数据冷热备,对于小公司来说要做好所有数据备份需要支出大量的成本,很多公司也是不现实的.万一还没有做好备份,数据被误删除 ...

  6. Java可变长参数方法调用问题

    不说废话,直接上代码: package mytest; import java.util.List; public class TestClass { public void method(List& ...

  7. HDU 3746:Cyclic Nacklace

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏

    Binary Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6355   Accepted: 2922 Descr ...

  9. Linq中疏漏的几个知识点

    1.Union - 连接不同集合,自动过滤相同项 2.Concat - 连接不同集合,不会自动过滤相同项 3.Select - 类似List的ConvertAll,转换集合成员 4.Enumerabl ...

  10. hiho(1081),SPFA最短路,(非主流写法)

    题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...