HDU 5723 Abandoned country (最小生成树+任意两点期望)

Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 7670    Accepted Submission(s): 1862


 

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 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

 

Author

HIT

 

Source

2016 Multi-University Training Contest 1

 

Recommend

wange2014   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

Statistic | Submit | Discuss | Note

题意:

  给定n个定点,m条边,求最小生成树以及任意两点之间距离的期望。

思路:

  任意两点的期望是

     (权值E【i】.C)*(这条路被走过的次数 now )的总和 (即桥所连接的点的个数的乘积)   除以  总共的路径数X。

  比如题目 从1到2距离为2,从2到3距离为3。那么从1到2走过2一次,从1到3走过2一次,走过3一次,从2到3走过3一次。

  所以期望为(2*2+3*2)/ 3 = 3.33。

  先求最小生成树值 sum

  应用深搜回溯,找出这条路被走过过少次。这样就可以求出期望了。

  这里由于数据量比较大,可以用Kruskal求最小生成树。

    注意最后求期望的时候总共的路径数量n*(n-1)/2数据量比较大,应该用long long 存储。

     代码:

#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<string.h>
using namespace std;
vector<pair<int,int> >  v[100010];

struct Edge
{
    int f,t,q;
};

int m,n;//n为村庄数,m为街道数
Edge s[1000010];//存储图
long long  ans;//存最后的每条路的总和
int pre[100010];//并查集的祖先数组
int vis[100010];//标记数组

bool cmp(Edge a,Edge b )//排序函数
{
    return a.q<b.q;
}

int Find(int x)//找祖先
{
    if(x!=pre[x])
    {
        pre[x]=Find(pre[x]);
    }
    return pre[x];
}

void Merge(int x,int y)//查是否相等
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
        pre[fx]=fy;
}

long long dfs(int x) //dfs递归搜索
{
    vis[x]=1;
    long long now=0,all=1;//now记录当前节点直接连接的节点数量  all记录此节点经过搜索后所有的与此节点连接的节点数
    int h=v[x].size();
    for(int i=0; i<h; i++)
    {
        int b=v[x][i].first;
        if(!vis[b])
        {
            now=dfs(b);
            all+=now;
            ans+=now*(n-now)*v[x][i].second;//ans记录的是权值*经过的次数
        }
    }
    return all;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        if(m==0||n==0)
            {printf("0 0.00");continue;}

        for(int i=0;i<=n;i++)
            v[i].clear();
        for(int i=0; i<=n; i++)//并查集的祖先节点的初始化
        {
            pre[i]=i;
        }
        for(int j=0; j<m; j++)//输入
        {
            scanf("%d%d%d",&s[j].f,&s[j].t,&s[j].q);
        }
        sort(s,s+m,cmp);//排序
        long long sum=0;//sum用来记录最小生成树的长度
        for(int j=0; j<m; j++)
        {
            int fx=Find(s[j].f);
            int fy=Find(s[j].t);
            if(fx!=fy)  //如果祖先不相等,那么加入到最小生成树中
            {
                sum=sum+s[j].q;
                Merge(fx,fy);
                //加入到动态数组中准备做期望
                v[s[j].f].push_back(make_pair(s[j].t,s[j].q));
                v[s[j].t].push_back(make_pair(s[j].f,s[j]. q));
            }
        }
        dfs(1);//深搜回溯计算ans的值
        double y=1.0*n*(n-1)/2;
        printf("%lld %.2lf\n",sum,(double)ans/y);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41668093/article/details/82988681