hdu 5723 Abandoned country 最小生成树 期望
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 最小生成树 期望的更多相关文章
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5723 Abandoned country 最小生成树+子节点统计
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country (最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...
随机推荐
- Matrix67|自由职业者,数学爱好者
Matrix67|自由职业者,数学爱好者 介绍一下你自己和所做的工作. 我叫顾森,网名 Matrix67,长住北京的重庆人,目前没有固定的职业.一会儿当当码农,一会儿做做编辑,一会儿教教数学,一会儿写 ...
- 【原创】backbone1.1.0源码解析之Events
最近在看些node的源代码,发现backbone的应用还是挺广泛的,但是之前的学习忘得一干二净了,后悔当时没做笔记啊. 所以,无奈想用的更好,就是得把源代码看楚,所以还是把源代码的注释笔记留下来,供自 ...
- php拾遗: 类型约束
突然间什么都不想干,感觉就像来大姨夫一样..但是又不能断了每个工作日都写博客的习惯..所以今天水一下吧. PHP用了快2年了,但是这东西竟然第一次看到,突然间,觉得自己有掉回战五渣的行列了.翻开官方文 ...
- CS229 笔记02
CS229 笔记02 公式推导 $ {\text {For simplicity, Let }} A, B, C \in {\Bbb {R}}^{n \times n}. $ $ {\bf {\t ...
- 第12月第2天 uiscrollview _adjustContentOffsetIfNecessary 圆角
1. uiscrollview在调用setFrame,setBounds等方法的时候会默认调用稀有api: _adjustContentOffsetIfNecessary 这个方法会改变当前的cont ...
- NOI2001 方程的解数(双向搜索)
solution 一道非常经典的双向搜索题目,先将前3个未知数枚举一遍得到方程的前半部分所有可能的值,取负存入第一个队列中再将后3个未知数枚举一遍,存入第二个队列中.这样我们只要匹配两个队列中相同的元 ...
- (F. MST Unification)最小生成树
题目链接:http://codeforces.com/contest/1108/problem/F 题目大意:给你n个点和m条边,然后让你进行一些操作使得这个图的最小生成树唯一,每次的操作是给某一条边 ...
- 在pycharm和tensorflow环境下运行nmt
目的是在pycharm中调试nmt代码,主要做了如下工作: 配置pycharm编译环境 在File->Settings->Project->Project Interpreter 设 ...
- Binary Representation
Given a (decimal - e.g. 3.72) number that is passed in as a string, return the binary representation ...
- elasticsearch安装ik分词器(极速版)
简介:下面讲有我已经打包并且编辑过的zip包,你可以在下面下载即可. 1.下载zip包.elasticsearch-analysis-ik-1.8.0.jar下面有附件链接[ik-安装包.zip],下 ...