HDU 5723:Abandoned country(最小生成树+算期望)
http://acm.hdu.edu.cn/showproblem.php?pid=5723
Abandoned country
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.
#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(最小生成树+算期望)的更多相关文章
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- hdu 5723 Abandoned country 最小生成树+子节点统计
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country (最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...
随机推荐
- C++ 异常机制分析(C++标准库定义了12种异常,很多大公司的C++编码规范也是明确禁止使用异常的,如google、Qt)
阅读目录 C++异常机制概述 throw 关键字 异常对象 catch 关键字 栈展开.RAII 异常机制与构造函数 异常机制与析构函数 noexcept修饰符与noexcept操作符 异常处理的性能 ...
- 一个JSP结果页面tomcat内存溢出
如今,试验组的同事寻找新能源我看到一个奇怪的现象.一个tomcat应用,内只有一个简单的jsp页面,和这个jsp无论是什么页java代码(我想用这个jsp在她的网页测试server一对tomcat的最 ...
- WPF中的文字修饰——上划线,中划线,基线与下划线
原文:WPF中的文字修饰——上划线,中划线,基线与下划线 我们知道,文字的修饰包括:空心字.立体字.划线字.阴影字.加粗.倾斜等.这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线.中划线 ...
- iPhone开发笔记(20)EGOImageView的使用方法及注意事项
EGOImageView是一种实现网络图片的异步加载和缓存的第三方类库,具有相同功能的第三方类库还有SDWebImage.但是相比两个类库的安装和使用来说,EGOImageView更简单一些,下面就介 ...
- 扩展ASCII码,不同的国家有不同的字符集。Unicode转换为utf8的规则,utf8没有大小端的问题。超过0xFFFF的Unicode字符WINAPI也无能为力(附各种字符编码表及转换表)good
一.概念 1,ASCII ASCII(American Standard Code for Information Interchange),中文名称为美国信息交换标准代码.是 ...
- 图像滤镜艺术---Photoshop实现Instagram之Sierra滤镜
原文:图像滤镜艺术---Photoshop实现Instagram之Sierra滤镜 本文介绍PS实现Instagram中的Sierra滤镜,内容如下: 先看下效果图: 下面是具体实现步骤: 1. To ...
- SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database)
原文:SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容(C ...
- 给变量赋值,程序会跳到 HardFault_Handler的问题
原因:变量属于指针,该指针没有初始化
- Dropbox是同步盘,Box.net是网盘(所以要学习Box)
自从能无缝用Dropbox后,确实得瑟了很久,但只有可怜巴巴的2G空间,搞不出什么妖蛾子,dropbox的好用,世所共知.百度云盘2T的空间,我却不敢把重要的东西放在里面. 在还没有优盘的时候,我常常 ...
- Delphi中,indy控件实现收发邮件的几点学习记录( 可以考虑加入多线程,用多个邮箱做一个邮箱群发器) 转
关于用Delphi中的Indy控件实现收发邮件的几点学习记录 这几天心里颇不宁静,不是因为项目延期,而是因为自己几个月前做的邮件发送程序至今无任何进展,虽然一向谦虚的人在网上发 ...