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(n100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000). 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(T10) 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
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(n1)/2就是答案。可以OnOnOn求出。任取一点为根dfs,对每个点iii记录其子树包含的点数(包括其自身),设点数为sum[i]sum[i]sum[i],则iii的父亲一侧的点数即为n−sum[i]n-sum[i]nsum[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