http://acm.hdu.edu.cn/showproblem.php?pid=5723

Abandoned country

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
 
题意:国家有M条废弃的路和N个点,现在要重新修,第一个问题是求出连通每一个点需要的最短路径是多少,第二个问题是求在连通的路任意选两个点走,它的最小期望是多少。
思路:第一个问题很简单,就是单纯的最小生成树,但是第二个问题一开始让我和队友感到很迷茫。英文题面,真是比较难懂意思。最小期望??什么鬼。。后来仔细想了好久,建立起了最小生成树的图了。。最终还是不会做,不知道DFS怎么跑来算期望。。听了别人的讲解才知道  一棵树上,设某一段路在用DFS遍历路径过程中在该条路径遍历过的次数为 cnt,例如有这样一条路径:1 -> 3 -> 5 -> 2 ,那么 路 1 -> 3 的cnt为3,路 3 -> 5 的cnt为2,路 5 -> 2 的cnt为1,这样。我们要算 路 1 -> 3 对答案的贡献,那么设 路 1 -> 3的权值为 w, 它的贡献就是 cnt * ( n - cnt ) * w , 然后期望就是 所有的路的贡献加起来 * 2 / ( n * ( n - 1 ) ) 。
这里用 Kruskal 算法比较好,复杂度低,也利于建最小生成树的图。
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
#define N 100005
#define M 1000005
typedef pair <int, int> P;
struct node
{
int u, v, w;
}e[M*];
vector <P> edge[N]; int n, m;
double ans2;
int fa[N]; bool cmp(const node& e1, const node& e2)
{
return e1.w < e2.w;
} int Find(int x)
{
if(fa[x] == x) return x;
return fa[x] = Find(fa[x]);
} void Union(int x, int y)
{
int fx = Find(x), fy = Find(y);
if(fx != fy) fa[fx] = fy;
} long long Kruskal()
{
for(int i = ; i <= n; i++)
fa[i] = i;
for(int i = ; i <= n; i++)
edge[i].clear(); sort(e, e + m*, cmp);
long long res = ;
for(int i = ; i < m*; i++) {
int u = e[i].u, v = e[i].v, w = e[i].w;
int fu = Find( u ), fv = Find( v );
if(fu != fv) {
Union( u, v );
res += e[i].w;
edge[u].push_back(P(v, w));
edge[v].push_back(P(u, w));
//建最小生成树的图
}
}
return res;
} int dfs(int u, int fa)
{
int cnt = ;
for(int i = ; i < edge[u].size(); i++) {
int v = edge[u][i].first , w = edge[u][i].second;
if( fa != v ) {
int now = dfs(v, u);
cnt += now;
ans2 = ans2 + 1.0 * now * (n - now) * w;
//算每段路的贡献
}
}
return cnt + ;
} int main()
{
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++) {
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
e[m+i].v = e[i].u;
e[m+i].u = e[i].v;
e[m+i].w = e[i].w;
} long long ans1 = Kruskal();
ans2 = ;
dfs(, -);
ans2 = ans2 * 2.0 / (1.0 * n) / (n - 1.0); printf("%I64d %.2f\n", ans1, ans2);
}
return ;
}

HDU 5723:Abandoned country(最小生成树+算期望)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  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)

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

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

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

随机推荐

  1. 优雅实现INotifyPropertyChanged接口——利用Lambda表达式

    原文:优雅实现INotifyPropertyChanged接口--利用Lambda表达式 参考文章 在14年的时候,曾经读过上面的参考文章,不过当时并没有怎么理解,慢慢地也就将这篇文章忘诸脑后了. 直 ...

  2. 日志文件 清理or压缩

    1.操作前请断开所有数据库连接. 2.分离数据库 分离数据库:企业管理器->服务器->数据库->cwbase1->右键->分离数据库 分离后,cwbase1数据库被删除, ...

  3. glibc_error reporting

    很多GNU C库里的函数都会侦测并报告错误条件.我们的程序需要检测这些错误条件.比如:我们打开一个输入文件时需要判断该文件是否正确的打开.如果没有正确打开,我们需要打印错误或者采取其他正确的方式.为了 ...

  4. selenium + ChromeDriver 实战系列之启信宝(一)

    之前写了一篇selenium + ChromeDriver的一些入门的知识,这篇博客里面找了启信宝这个网站,简单的进行了一个实战练习.本篇博客的结构如下:       首先会给出一些使用seleniu ...

  5. Python标准库(3.x): itertools库扫盲

    itertools functions accumulate() compress() groupby() starmap() chain() count() islice() takewhile() ...

  6. ArchLinux 安装记录

    主要步骤 下载镜像及刻录 开机安装 联网 编辑镜像站文件 分区 格式化分区并挂载 安装基本操作系统 配置基础操作系统 引导系统 用户管理 网络配置 安装Gonme桌面环境 其他优化 开始准备 下载镜像 ...

  7. hMailServer搭建简单邮件系统

    本文介绍的是搭建本地的邮件系统,至于互联网的还在研究之中. 1.需要一个邮件服务器软件,这里用的是hMailServer,其中会让你设置一个密码,记住这个密码,后面连接的时候回用到. 2.添加域名 因 ...

  8. 逻辑回归模型(Logistic Regression)及Python实现

    逻辑回归模型(Logistic Regression)及Python实现 http://www.cnblogs.com/sumai 1.模型 在分类问题中,比如判断邮件是否为垃圾邮件,判断肿瘤是否为阳 ...

  9. Delphi事件的广播

    原文地址:Delphi事件的广播 转作者:MondaySoftware 明天就是五一节了,辛苦了好几个月,借此机会应该尽情放松一番.可是想到Blog好久没有写文章,似乎缺些什么似的.这几个月来在项目中 ...

  10. Markdown的选择

    直击现场 我一直在思索用什么格式存储文档比较好.之前一直在用google docs,但是它的格式不公开,上传/下载的时候需要转换格式,转换的时候必然会丢失一些信息.于是后来想,那还是纯本文或者mark ...