(HDU 5723)Abandoned country <最小生成树 + 树上所有两点之间的距离的期望> 多校训练1

Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4941 Accepted Submission(s): 1249

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

题意:
给你一个图。求最小生成树的权值,并任意取两点,求两点之间的距离的期望值。

分析:
由于每条边的权值都不同,所以保证了两点之间的最短路就是在最小生成树上。
对于求最小生成树的权值,我们直接用kruskal算法即可。
关于求期望,由于n<= 100000所以不能用floyd,其实也没有那个必要,太耗时了
对于所有两点之间的距离每条边经过的次数等于以这条边某一节点为根节点的子树上的节点个数tmp,乘上n-tmp
而所有的起点和终点的选择的种类有n*(n-1)种,总共有n*(n-1)/2条路
所以每条边经过的概率为 (tmp*(n-tmp))/(n*(n-1)/2)
所以只要将生成树上的所有边乘上概率然后相加即可。

AC代码:

#include <iostream>  
#include <cstdio>  
#include <cmath>  
#include <queue>  
#include <cstring>  
#include <string>  
#include <queue>  
#include <algorithm>  
#include <iomanip>  
#include <vector>  
#include <set>  
#include <map>  
#define eps 1e-8  
#define LL long long  
#define sz1 1000010  
#define sz2 100010  
using namespace std;  
struct Edge  
{  
    int fm,to,cost,nxt;  
}E[sz2<<1];  
struct edge  
{  
    int fm,to,cost;  
}store[sz1];  
int cnt=0,n,m;  
int fa[sz2],head[sz2];  
LL cost;  
double ans=0;  
void addedge(int u,int v,int c)  
{  
   E[cnt].nxt=head[u];  
   head[u]=cnt;  
   E[cnt].fm=u;  
   E[cnt].to=v;  
   E[cnt++].cost=c;  
}  
bool cmp(edge a,edge b)  
{  
    return a.cost<b.cost;  
}  
//并查集操作  
int Find(int x)  
{  
    return  fa[x]!=x?fa[x]=Find(fa[x]):x;  
}  
void Union(int x,int y)  
{  
   fa[x]=y;  
}  
//计算期望  
int dfs(int x,int pre)  
{  
    int res=1,tmp;  
    for(int i=head[x];~i;i=E[i].nxt)  
    {  
        //不回去  
        if(E[i].to!=pre)  
        {  
            tmp=dfs(E[i].to,x);  
            ans+=1.0*tmp*(n-tmp)*E[i].cost;  
            res+=tmp;  
        }  
    }  
    //res为该节点为根节点的子树上的节点数  
    return res;  
}  
int main()  
{  
    int t,u,v,x,y,c,am;  
    scanf("%d",&t);  
    while(t--)  
    {  
        ans=0;  
        cnt=am=0;  
        cost=0;  
        memset(head,-1,sizeof(head));  
        scanf("%d%d",&n,&m);  
        for(int i=1;i<=n;i++)  
            fa[i]=i;  
        for(int i=0;i<m;i++)  
            scanf("%d%d%d",&store[i].fm,&store[i].to,&store[i].cost);  
        sort(store,store+m,cmp);  
        //寻找最小生成树  
        for(int i=0;i<m;i++)  
        {  
            u=store[i].fm;  
            v=store[i].to;  
            c=store[i].cost;  
            x=Find(u);  
            y=Find(v);  
            if(x!=y)  
            {  
                Union(x,y);  
                am++;  
                cost+=store[i].cost;  
                addedge(u,v,c);  
                addedge(v,u,c);  
                //已经添加了n-1条边,则可以停止  
                if(am==n-1)  
                    break;  
            }  
        }  
        dfs(1,-1);  
        printf("%lld %.2lf\n",cost,2*ans/(1LL*n*(n-1)));  
    }  
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值