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
Author
HIT
Source
2016 Multi-University Training Contest 1

题目大意:

有n个点,有m条边,让你求它的最小生成树,并且在这个生成树上,求任意两点间的距离的期望。

解题思路:

第一问比较裸,就直接求最小生成树就好,第二问需要一些技巧。

现在有一条边ab,其中b的子树个数为num,那么这条边被经过的次数为(n - num) * num

然后乘一下这条边的权值就可以了

代码:

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;const int maxn = 1e5 + 5;typedef long long LL;
typedef struct node{int from, to;int w;bool operator < (node zz){return w < zz.w;}
}Edge;
typedef struct nn{int to;int w;nn(){}nn(LL a, LL b){to = a; w = b;}
}EEE;LL ans;
double res;
int t, n, m;
Edge g[maxn * 10];
vector<EEE> e[maxn];
int vis[maxn], pre[maxn];int findfather(int x){return pre[x] = (pre[x] == x ? x : findfather(pre[x]));
}
void join(int x, int y){pre[findfather(x)] = findfather(y);
}
void kruskal(){int x, y, flag = 0;for(int i = 0; i < m; ++i){x = findfather(g[i].from);y = findfather(g[i].to);if(x == y) continue;join(x, y);++flag;ans += g[i].w;e[g[i].from].push_back(nn(g[i].to, g[i].w));e[g[i].to].push_back(nn(g[i].from, g[i].w));if(flag == n - 1) return;}
}
int dfs(int p){if(vis[p]) return 0;vis[p] = 1;int tt = 1, num;for(int i = 0; i < e[p].size(); ++i){int v = e[p][i].to;num = dfs(v);tt += num;res += (LL)(n - num) * (LL)num * (LL)e[p][i].w;}return tt;
}
int main(){scanf("%d", &t);while(t--){scanf("%d%d", &n, &m);for(int i = 1; i <= n; ++i) {pre[i] = i;vis[i] = 0;e[i].clear();}for(int i = 0; i < m; ++i){scanf("%d%d%d", &g[i].from, &g[i].to, &g[i].w);}sort(g, g + m);ans = 0;kruskal();res = 0;dfs(1);printf("%lld %.2lf\n", ans, res / ((n - 1LL) * n / 2.0));}return 0;
}

转载于:https://www.cnblogs.com/wiklvrain/p/8179452.html

HDU-5723 Abandoned country相关推荐

  1. hdu-5723 Abandoned country(最小生成树+期望)

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

  2. Abandoned country

    A Abandoned country 最小生成树 + 树上任意点对距离之和 树上任意点对距离之和: 计算时考虑每一个边,它的贡献值为它的 左端点以左的点的个数 * 右端点以右的点的个数 * 该边的权 ...

  3. 2019.9.17最小生成树知识点回顾

    POJ 1797 Heavy Transportation(最大生成树-Prim) 最大生成树,方法模仿最小生成树,每次选最大边进行操作,即可. HDU 5723 Abandoned country( ...

  4. Kuangbin专题三Dancing Links

    Kuangbin专题三Dancing Links 没写完所有的,因为要去上课了赶紧先预习一下,这就先发出来吧. 跳舞链这东西以前在hihocoder上翻到过,当时看的模模糊糊的,现在好好学一学. 暂时 ...

  5. 暑期集训3:几何基础 练习题G: HDU - 1052

    2018学校暑期集训第三天--几何基础 练习题G  --   HDU - 1052   (昨天加练题) Tian Ji -- The Horse Racing Here is a famous sto ...

  6. hdu 4857 Little Devil I

    http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,边的颜色要么为白色,要么为黑色,初始每条边为白色,有三种操作 1.将u-v链上面的所有 ...

  7. hdu 2433 Travel

    http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

最新文章

  1. opencv轮廓周长原理_OpenCV3 - 轮廓特征
  2. 6显示wan口未插线_WiFi 6到底6不6——华硕 AX56U热血版 路由器评测
  3. 语言筛选法选素数怎么解决输出超限_论文深耕 | 什么时候才能把文献看完?——论文的正确筛选、阅读顺序...
  4. 实现仿简书选取内容生成分享图片效果
  5. 邮件服务器实用技巧和选购指南
  6. 软件开发人员,自身素质应该注意的问题!
  7. CLR寄宿(上) MSCOREE.DLL
  8. 嵌入式C语言(指针)
  9. Dos攻击与DDos攻击
  10. 微信小程序入门12-微信小程序开发设置中服务器域名和业务域名
  11. 如何看待”日光之下,并无新事“
  12. Windows自带的【远程桌面连接】操作方法及常见问题
  13. 部分双机热备软件详细介绍-行云管家
  14. 计算机组装的硬件配置及报价,2021组装电脑配置清单及价格表说明
  15. Kunbernetes——二进制单节点部署
  16. CODING 携手 Thoughtworks 助力老百姓大药房打造”自治、自决、自动”的敏捷文化
  17. 智慧小区智慧物业管理系统一体化解决方案
  18. 【ModuleNotFoundError 与 ImportError】之 most likely due to a circular import
  19. 年薪170W阿里P8相亲要求女方月薪1万,网友:有点高
  20. android 指定区域内绘制,Android实现扫一扫功能之绘制指定区域透明区域

热门文章

  1. CentOS6.4 添加播放×××
  2. 读写配置文件app.config
  3. oracle 多条执行语句同时执行
  4. javascript--this总结
  5. .net中语音识别和语音合成(二)语音合成提高篇
  6. 认真点,带你全面了解xutils3
  7. C语言基本数据结构之三(图的广度及深度遍历,求单源最短路径的Dijkstra算法)
  8. Binder相关面试总结(一):为什么Android要采用Binder作为IPC机制?
  9. 算法------------ 最长公共前缀
  10. 算法---------数组-----------两数相加