题目说每条边权值都不一样,说明最小生成树是唯一的,不存在最小期望这一说。

然后就是先求出最小生成树,随便确定一个根节点,计算出每个点的子树有多少节点,记为c[x]。

指向x的这条边被统计的次数为c[x]*(n-c[x])。然后基本就可以算出答案了。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL; const int maxn = + ;
int T, n, m, f[maxn], c[maxn];
bool flag[maxn];
struct Edge { int u, v; LL cost; }e[ * maxn], ee[maxn];
vector<int>g[maxn];
double fz, fm; int Find(int x) { if (x != f[x]) f[x] = Find(f[x]); return f[x]; }
bool cmp(const Edge&a, const Edge&b) { return a.cost < b.cost; } void dfs(int x,int f)
{
flag[x] = ; c[x] = ;
for (int i = ; i < g[x].size(); i++)
{
int id = g[x][i]; if (flag[ee[id].v]) continue;
dfs(ee[id].v,id); c[x] = c[x] + c[ee[id].v];
}
if (f != -)
fz = fz + 1.0*ee[f].cost*1.0*c[x] * 1.0* (n - c[x]);
} int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++)
scanf("%d%d%lld", &e[i].u, &e[i].v, &e[i].cost);
sort(e + , e + + m, cmp);
LL ans = ;
for (int i = ; i <= n; i++) f[i] = i, g[i].clear();
int sz = ;
for (int i = ; i <= m; i++)
{
int fx = Find(e[i].u), fy = Find(e[i].v);
if (fx != fy)
{
f[fx] = fy, ans = ans + e[i].cost;
ee[sz].u = e[i].u, ee[sz].v = e[i].v, ee[sz].cost = e[i].cost;
g[e[i].u].push_back(sz++);
ee[sz].u = e[i].v, ee[sz].v = e[i].u, ee[sz].cost = e[i].cost;
g[e[i].v].push_back(sz++);
}
}
memset(c, fz=, sizeof c);
memset(flag, fm=, sizeof flag);
dfs(, -);
fm = 1.0*n*1.0*(n - ) / 2.0;
printf("%lld %.2lf\n", ans, fz / fm);
}
return ;
}

HDU 5723 Abandoned country的更多相关文章

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

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

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

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

  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 【最小生成树&&树上两点期望】

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. zonghe

      package hcxAction; import hcxMode.Advertises; import hcxMode.Areas; import hcxMode.Saveresume; imp ...

  2. Neutron 不健全的HA ROUTER

    首先介绍下HA,所谓的HA就是高可用性,但HA有双主.主备两种工作模式,其中主备模式又包含抢占与 非抢占两种方式,而Neutron Router采用的无疑是HA中最简单的工作方式非抢占主备模式. HA ...

  3. web开发路径问题

    1. web开发路径问题总结: http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html 2. JSP/SERVLET 路径问题 ...

  4. iOSstoryboard xib下label怎么自适应宽度高度

    先看需求:两个Label,要求蓝色的label紧跟在红色的label文字后面 ok首选正常添加约束 红色的Label添加宽度,高度,左边,上边约束 蓝色的Label添加宽度,高度,左边,和红色的水平对 ...

  5. 其他应用和技巧-eval()函数大行其道

    ---------------------------------- <script type="text/javascript">                   ...

  6. python paramiko基本

    基本 import paramiko blade = '192.168.0.10' port = 22 username = 'admin' password = ' localpath = '/ho ...

  7. Canvas 数学、物理、动画学习笔记一

    Canvas 第五章 数学.物理和运动学习笔记让人映像深刻的运动,需要我们不只是简单的知道如何移动对象,还需要知道怎么按用户期望看到的方式去移动它们.这些需要基于数学知识的基本算法和物理学作用.基于点 ...

  8. Ajax交互,浏览器接收不到服务器的Json数据(跨域问题)

    该问题的情景如下: 问题描述 Ajax的请求代码放在一台机器上,而服务器的java 路由程序放在另一个机子上,所以Ajax的url填写的是带"http://"  的地址,而不是相对 ...

  9. ubuntu 自动获取ip的怎么设置

    ubuntu以DHCP方式配置网卡自动获取ip编辑文件/etc/network/interfaces:sudo vi /etc/network/interfaces并用下面的行来替换有关eth0的行: ...

  10. 依赖注入(DI)和控制反转(IOC)【回顾】

    在java开发中广泛的使用了IOC的思想,在PHP中同样也在广泛使用. interface Coder { public function coding(); } 实现类Javaer class Ja ...