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 ...
随机推荐
- Entity framework 更改模型,新增表
在Package Manager Console 中运行命令Enable-Migrations 再次运行可以更新 抄袭 在实体类中增加一个属性以后,执行 Update-Database 命令 ,可以更 ...
- 潜移默化学会WPF--值转换器
原文:潜移默化学会WPF--值转换器 1. binding 后面的stringFormat的写法----连接字符串 <TextBlock Text="{Binding Path=Qty ...
- linux没有 conio.h解决的方式
conio.h不是C标准库中的头文件,在ISO和POSIX标准中均未定义. conio是Console Input/Output(控制台输入输出)的简写,当中定义了通过控制台进行数据输入和数据输出的函 ...
- WPF 4 TextBox 笔刷特效
原文:WPF 4 TextBox 笔刷特效 TextBox 控件是我们开发过程中必不可少的组件,它可以使应用程序方便的与用户进行文字交互.在新WPF 4 中又为TextBox 添加了两种新笔 ...
- 动态lambda 构建
var param = Expression.Parameter(typeof(T)); var datetime1 = Expression.Constant(dt1); var datetime2 ...
- excel操作for(lutai)
条件统计某个区域的值 第一种方法: =SUMIFS(P2:P5,L2:L5,A2) 第一个参数:被求和的单元格范围 第二个参数:明细表条件值单元格范围 第三个参数:主表条件单元格(可以是范围) 公式的 ...
- 深度网络中的Tricks
数据增强(Data augmentation) 预处理(Pre-processing) 初始化(Initializations) 训练中的Tricks 激活函数(Activation function ...
- Oracle emca on linux
http://blog.csdn.net/haibusuanyun/article/details/16338591 bash-3.2$ lsnrctl status LSNRCTL for Lin ...
- 深入解析Windows窗口创建和消息分发(三个核心问题:怎么将不同的窗口过程勾到一起,将不同的hwnd消息分发给对应的CWnd类去处理,CWnd如何简单有效的去处理消息,由浅入深,非常清楚) good
笔记:争取不用看下面的内容,只看自己的笔记,就能记住这个流程,就算明白了: _tWinMain-->AfxWinMain,它调用四个函数: -->AfxWinInit用于做一些框架的初始化 ...
- Linux升级OpenSSL版本
安装nginx的时候,出现了这样的问题: nginx : Depends: libssl1.0.0 (>= 1.0.2~beta3) but 1.0.1f-1ubuntu2.11 is to b ...