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

然后就是先求出最小生成树,随便确定一个根节点,计算出每个点的子树有多少节点,记为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. genymotion模拟器配置Genymotion-ARM-Translation 兼容包

    前提是你的adb的环境已经配置正确,不知道怎么配置的可参考http://jingyan.baidu.com/article/17bd8e52f514d985ab2bb800.html 如果还不成功的话 ...

  2. E - Triangle

    Description Johnny has a younger sister Anne, who is very clever and smart. As she came home from th ...

  3. html5游戏开发框架之lufylegend开源库件学习记录

    下载地址http://lufylegend.com/lufylegend 引用 <script type="text/javascript" src="../luf ...

  4. 201312月CCF-2,ISBN号码分析

    明天要考CCF啦,偶还是很紧张的.最近看了数据结构,今天才开始上机练习,对,我就是这么懒..废话不多说,我写这篇文章主要是分析CCF编程的小窍门,因为在网上没找到,所以我决定自力更生丰衣足食.!!!! ...

  5. 一个初学者的辛酸路程-初识Python-1

    前言 很喜欢的一句话,与诸位共勉. 人的一切痛苦,本质上都是对自己无能的愤怒----王小波. 初识Python 一.它的爸爸是谁 首先,我们需要认识下面这位人物. 他是Python的创始人,吉多范罗苏 ...

  6. php 即时输出

    <?phpset_time_limit(0);ob_end_clean();ob_implicit_flush(1);$i=0;while ( $i>=0 ) { sleep(1); ec ...

  7. WebDriver使用IE和chrome浏览器

    因为我用的是 selenium-dotnet-2.47.0.zip IEDriverServer_x64下载地址 https://code.google.com/p/selenium/download ...

  8. Q promise API简单翻译

    详细API:https://github.com/kriskowal/q/wiki/API-Reference Q提供了promise的一种实现方式,现在在node中用的已经比较多了.因为没有中文的a ...

  9. hdu_1181_变形课(dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 题意:中文题,不解释 题解:直接DFS #include<cstdio> #incl ...

  10. a/b + c/d

    a/b + c/d Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...