Abandoned country

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5723

Description

An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). Guaranteed that any two wi are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk.

Input

The first line contains an integer T(T≤10) which indicates the number of test cases.

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.

Output

output the minimum cost and minimum Expectations with two decimal places. They separated by a space.

Sample Input

1

4 6

1 2 1

2 3 2

3 4 3

4 1 4

1 3 5

2 4 6

Sample Output

6 3.33

Hint

题意

给你一个无向图,保证边权都不相同,让你求一棵最小生成树。

然后说有一个人在这棵树上瞎选起点和终点,问这个人走路的期望是多少。

题解:

典型的两道傻逼题杂糅在一起的题……

最小生成树跑kruskal

期望的话,考虑边(u,v,c),那么对答案贡献就是size[u] * (n-size[u]) * c * n(n-1)/2

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int n,m;
int fa[maxn];
double ans2;
vector<pair<int,int> >E[maxn];
int fi(int u){
return u != fa[u] ? fa[u] = fi( fa[u] ) : u;
}
void uni(int x,int y){
int p=fa[x],q=fa[y];
if(p==q)return;
fa[p]=q;
}
struct node{
int x,y,z;
}p[maxn];
bool cmp(node a,node b){
return a.z<b.z;
}
void init(){
ans2=0;
for(int i=0;i<maxn;i++)fa[i]=i;
memset(p,0,sizeof(p));
for(int i=0;i<maxn;i++)E[i].clear();
}
int dfs(int x,int f){
int sz = 0;
for(int i=0;i<E[x].size();i++){
int v = E[x][i].first;
if(v==f)continue;
int tmp = dfs(v,x);
sz+=tmp;
ans2 = ans2 + 1.0 * tmp * (n-tmp) * E[x][i].second;
}
return sz+1;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
}
sort(p+1,p+1+m,cmp);
long long ans = 0;
for(int i=1;i<=m;i++){
int p1=fi(p[i].x),q1=fi(p[i].y);
if(p1==q1)continue;
uni(p1,q1);
ans+=p[i].z;
E[p[i].x].push_back(make_pair(p[i].y,p[i].z));
E[p[i].y].push_back(make_pair(p[i].x,p[i].z));
}
dfs(1,0);
ans2 = ans2 * 1.0 * 2.0 * 1.0 / (1.0* n) / (n - 1.0);
printf("%I64d %.2lf\n",ans,ans2);
}
}

hdu 5723 Abandoned country 最小生成树 期望的更多相关文章

  1. HDU 5723 Abandoned country 最小生成树+搜索

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

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

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

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

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

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

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

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

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

  6. HDU 5723 Abandoned country 【最小生成树&&树上两点期望】

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

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

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

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

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

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

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

随机推荐

  1. hdu 5181 numbers

    http://acm.hdu.edu.cn/showproblem.php?pid=5181 题意: 有一个栈,其中有n个数1~n按顺序依次进入栈顶,在某个时刻弹出. 其中m个限制,形如数字A必须在数 ...

  2. Linux之svn数据备份、还原及迁移

    前言 因管理需求现要将svn数据进行备份,作为运维小哥的我在收到指令后进行了相关操作.当然,领导告知的是要备份,但作为一个有思想的运维,我考虑到的是自己要干的不仅仅是备份操作,还要确保在备份后数据还原 ...

  3. 关于 xcode5 的no matching provisioning profiles found

    CHENYILONG Blog 关于 xcode5 的no matching provisioning prof- about the question in xcode5 "no matc ...

  4. 多源复制遇到CREATE USER FAILED错误

    MySQL Multi-Source Replication enables a replication slave to receive transactions from multiple sou ...

  5. 记一次ThreadPoolExecutor面试

    ThreadPoolExecutor点滴 线程池应该也是面试绕不开的一个点,平时大家也没少用,但其实也有一些小Tips还是值得记录一下. Constructor public ThreadPoolEx ...

  6. QTP设置共享对象库

    第一步:把需要加到共享对象库中的各个用例脚本的对象库,分别导出成.tsr文件. 操作方法:先用QTP打开已经录制完毕的脚本后,选择Resources-->Object Repository.然后 ...

  7. linux du查询目录所占的磁盘空间

    linux查询目录所占的磁盘空间 du -hxs /* --exclude=/proc |sort -rh 命令和选项的解释: du – 估计文件的空间使用情况 -hsx – (-h)更易读的格式,( ...

  8. pip --version问题

    安装pip之后,在cmd下输入 pip --version始终提示: Unknown option:versionDid not provide a command自己安装步骤没错,怎么想也不明白,无 ...

  9. 如何使用windows的计划任务?计划任务?

    我们经常有一些程序想要过了几小时来运行:比如定时关机 或者说希望能够每天的几点执行一个什么程序: 这些所有的操作都需要用到windows的任务计划:或者叫计划任务:反正都一样 下面小编将指导大家创建一 ...

  10. Ubuntu下mysql使用

    1. 从网上安装 sudo apt-get install mysql-server.装完已经自动配置好环境变量,可以直接使用mysql的命令. 注:建议将/etc/apt/source.list中的 ...