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

然后就是先求出最小生成树,随便确定一个根节点,计算出每个点的子树有多少节点,记为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. iOS开发的一些奇巧淫技(转载)

    iOS开发的一些奇巧淫技 http://www.cocoachina.com/ios/20141229/10783.html iOS开发的一些奇巧淫技2 http://www.cocoachina.c ...

  2. (╭ ̄3 ̄)╭ 小希的迷宫II

    (╭ ̄3 ̄)╭ 小希的迷宫II TimeLimit: 2000/1000 MS (Java/Others)  MenoryLimit: 65536/32768 K (Java/Others) 64-b ...

  3. TCP/IP 和HTTP 协议基础知识

    来源:http://www.myhack58.com/Article/60/63/2014/50072.htm 相信不少初学手机联网开发的朋友都想知道Http与Socket连接究竟有什么区别,希望通过 ...

  4. C#采用OpenXml给word里面插入图片

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  5. 通过httplib2 探索的学习的最佳方式

    在工作中需要对一个视频点播两百次,使其成为热门视频,才能对其p2p情况进行测试.虽然可以手动点播两百次,但是利用python发送200次post请求,能减少很多的工作量.该发送请求的方法用到了http ...

  6. linux下ClamAV使用

    第一步:Clamav下载http://www.clamav.net/downloads#yuminstall wget –y第二步:创建clamav用户和组groupaddclamav   (创建cl ...

  7. android开发技巧

    1 Android去掉listView,gridView等系统自带阴影 当我们使用listView的时候,拉到顶,或是拉到底部的时候,我们会发现有系统自带的阴影效果出现,不同手机出现的颜色可能还会不一 ...

  8. μCos-ii学习笔记2_任务管理

    二.任务管理 任务管理是ucos-ii操作系统的核心内容.这一章大致就以下流程来介绍和总结任务管理的相关知识. 要实现复杂任务管理,必然要定义众多数据来描述任务状态,为了精简,建立了许多不同的数据结构 ...

  9. 面向对象的特性-利用prototype为类创建静态成员

    —————————————————————————— <script type="text/javascript">            //用function模拟一 ...

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

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