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. OpenGL ES 2.0 Shader 调试新思路(二): 做一个可用的原型

    OpenGL ES 2.0 Shader 调试新思路(二): 做一个可用的原型 目录 背景介绍 请参考前文OpenGL ES 2.0 Shader 调试新思路(一): 改变提问方式 优化 ledCha ...

  2. python 喜马拉雅 音乐下载 演示代码

    1.主程序文件 import os import json import requests from contextlib import closing from progressbar import ...

  3. ASP.NET MVC学习笔记-----Filter(1)

    Filter类型 接口 MVC的默认实现 Description Authorization IAuthorizationFilter AuthorizeAttribute 最先执行,在其他类型的fi ...

  4. zTree的简单例子

    <%@ page language="java" pageEncoding="UTF-8" %> <%@ include file=" ...

  5. Xgboost理解

    一.xgboost模型函数形式 xgboost也是GBDT的一种,只不过GBDT在函数空间进行搜索最优F的时候,采用的是梯度下降法也就是一阶泰勒展开:而xgboost采用的是二阶泰勒展开也就是牛顿法, ...

  6. 【转载】maven pom详解(2)

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

  7. 优化MySQL的21个建议 – MySQL Life【转】

    今天一个朋友向我咨询怎么去优化 MySQL,我按着思维整理了一下,大概粗的可以分为21个方向. 还有一些细节东西(table cache, 表设计,索引设计,程序端缓存之类的)先不列了,对一个系统,初 ...

  8. K-means聚类算法的三种改进(K-means++,ISODATA,Kernel K-means)介绍与对比

      一.概述 在本篇文章中将对四种聚类算法(K-means,K-means++,ISODATA和Kernel K-means)进行详细介绍,并利用数据集来真实地反映这四种算法之间的区别. 首先需要明确 ...

  9. #Plugin 环形loading插件

    CircleLoader 环形loading插件 1.原生JS,不依赖jquery,zepto 2.前端学习交流群:814798690 案例展示 下载地址 https://github.com/cha ...

  10. 【BZOJ】3456: 城市规划(多项式求ln)

    题解 在我写过分治NTT,多项式求逆之后 我又一次写了多项式求ln 我们定义一个数列的指数型生成函数为 \(\sum_{i = 0}^{n} \frac{A_{i}}{i!} x^{i}\) 然后这个 ...