hdu5723(16多校第1场,树上两点平均距离的期望)
来源:互联网 发布:计量经济学软件 编辑:程序博客网 时间:2024/06/05 07:44
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1256 Accepted Submission(s): 328
Problem 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 integersn,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 .
For each test case, the first line contains two integers
Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
Sample Input
14 61 2 12 3 23 4 34 1 41 3 52 4 6
Sample Output
6 3.33
首先注意到任意两条边的边权是不一样的,由此得知最小生成树是唯一的,最小生成树既然 是唯一的,那么期望其实也就是唯一的,不存在什么最小期望。求完最小生成树之后,接下 来的问题就可以转换成在最小生成树上求任意两点之间距离的平均值,对于每条边,统计所 有的路径用到此边的次数,也就是边的两端的点数之积。那么这条边的总贡献就是次数*边 权。最后得到所有边的贡献之和再除以总路径数n∗(n−1)/2n*(n-1)/2n∗(n−1)/2就是答案。可以OnOnOn求出。任取一点为根dfs,对每个点iii记录其子树包含的点数(包括其自身),设点数为sum[i]sum[i]sum[i],则iii的父亲一侧的点数即为n−sum[i]n-sum[i]n−sum[i]。一边遍历一边统计就行。
#include <iostream>#include<string.h>#include<vector>#include<queue>#include<algorithm>#include<stdio.h>using namespace std;const int N=1000010,oo=1e9;typedef long long ll;int f[N];struct Node{ int v,len;};vector<Node>tu[N];double dp[N];int sum[N];void init(int n){ for(int i=0; i<=n; i++) f[i]=i,tu[i].clear();}int fa(int x){ if(x!=f[x]) return f[x]=fa(f[x]); return f[x];}struct hh{ int a,b,w;}xx[N*10];bool cmp(hh a,hh b){ return a.w<b.w;}void dfs(int root,int father,int n){ sum[root]=1; for(int i=0,l=tu[root].size(); i<l; i++) { int son=tu[root][i].v; int len=tu[root][i].len; if(son==father)continue; dfs(son,root,n); sum[root]+=sum[son]; double tem=((double)sum[son]*(double)(n-sum[son]))*(double)len; dp[root]+=dp[son]+tem; }}int main(){ int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); init(n); for(int i=0; i<m; i++) scanf("%d%d%d",&xx[i].a,&xx[i].b,&xx[i].w); sort(xx,xx+m,cmp); ll ans=0; int k=0; for(int i=0; i<m; i++) { int f1=fa(xx[i].a),f2=fa(xx[i].b); if(f1!=f2) { Node x1,x2; x1.v=xx[i].b,x1.len=xx[i].w; x2.v=xx[i].a,x2.len=xx[i].w; tu[xx[i].a].push_back(x1); tu[xx[i].b].push_back(x2); f[f1]=f[f2]; ans+=(ll)xx[i].w; k++; } if(k==n-1) break; } memset(sum,0,sizeof(sum)); memset(dp,0,sizeof(dp)); dfs(1,0,n); double tt=((double)n*(double)(n-1))/2.0; printf("%I64d %.2f\n",ans,dp[1]/tt); } return 0;}
0 0
- hdu5723(16多校第1场,树上两点平均距离的期望)
- (HDU 5723)Abandoned country <最小生成树 + 树上所有两点之间的距离的期望> 多校训练1
- HDU 2376 Average distance(树上两点间的期望)
- hdu5723 最小生成树+DFS(距离和,期望)
- hdu2586 树上两点之间的距离 tarjan
- 树上随机游走的期望距离
- hdu 2376(求树上任意两点之间距离之和的平均值)
- POJ 1986 Distance Queries LCA树上两点的距离
- 动态修改询问树上两点距离 poj2763
- POJ 2874 LCA 树上任意两点距离
- 第一场个人训练赛——B-计算两点间的距离(HDU 2001)
- HDU2376Average distance(树形dp|树上任意两点距离和的平均值)
- hdu5723 Abandoned country(最小生成树+期望)(2016多校第一场A题)
- HDU 4035 树上的期望
- 两点之间的距离(acm)
- hdu0j2001(计算两点间的距离)
- 计算两点间的距离(2001)
- 计算两点之间的距离(面试)
- OpenCV实现FloodFill泛洪填充算法的代码及相关函数详解
- LeetCode Sum of Two Integers
- 完全理解Android中的RemoteViews
- 在SQLserver中stuff 函数用法
- 【HDU】5468 Puzzled Elena
- hdu5723(16多校第1场,树上两点平均距离的期望)
- 监听开关机,发送广播
- Codeforces Round #363 (Div. 2) C dp
- uva 401 回文词
- 可变參函数设计
- 对话机器学习大神Michael Jordan:简介
- React Native开发android应用
- dddd
- Python 读文件