2016 Multi-University Training Contest 1 1001 Abandoned country

来源:互联网 发布:淘宝价格怎么设置打折 编辑:程序博客网 时间:2024/06/07 03:57

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723

题目:

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<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<vector>#define N 100010#define M 1000010#define ll long longusing namespace std;struct node{    int u,v;    ll w;}e[M];int n,m,pre[N],sum[N],c[N];vector<int> r[N];bool cmp(node a,node b ){    return a.w<b.w;}int findset(int v){    return v==pre[v]?v:pre[v]=findset(pre[v]);}void dfs(int t,int fa){    sum[t]=1;    for(int i=0;i<r[t].size();i++)    {        int u=r[t][i];        if(u!=fa)        {            dfs(u,t);            sum[t]+=sum[u];        }    }}void kruscal(){    sort(e,e+m,cmp);    for(int i=1;i<=n;i++)   pre[i]=i,r[i].clear();    int t=0;    ll ans=0;    for(int i=0;i<m;i++)    {        int u=e[i].u,v=e[i].v;        int t1=findset(u);        int t2=findset(v);        if(t1!=t2)        {            ans+=e[i].w;            pre[t1]=t2;            c[t++]=i;            r[u].push_back(v);            r[v].push_back(u);        }        if(t==n-1)  break;    }    printf("%lld ",ans);    ans=0;    dfs(1,-1);    for(int i=0;i<t;i++)    {        int tp=min(sum[e[c[i]].u],sum[e[c[i]].v]);        ans+=(ll)tp*e[c[i]].w*(n-tp);    }    printf("%.2f\n",(double)ans/n/(n-1)*2);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(int i=0;i<m;i++)        {            scanf("%d%d%lld",&e[i].u,&e[i].v,&e[i].w);        }        kruscal();    }}



0 0