HDU5723(2016多校第一场)——Abandoned country(最小生成树+dfs)

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

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


题意:就是求最小生成树,还有随机取两个点,求点之间权值的期望。

思路:先说注意点:一定要记得开long long!!!我就是因为这个WA了两发     

最小生成树怎么求不说了,个人习惯用krustal。

把最小生成树求出来以后,dfs一遍,求出每个点的子节点个数,怎么求期望呢?期望就是所有路径的权值和除以C(n,2)。

那么问题转化成了,怎么求一条边,在记录所有路径时被算了几次。

因为这是一颗最小生成树,可以把一条边看做桥,那么这条边被算过的次数就是两边顶点数相乘。也就是son[v]*(n-son[v])。画个图就很清楚了。


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

const int INF=1000000007;
const int MAXN =100010;
const int MAXM=1000010;

int par[MAXN];
int r[MAXN];

//并查集
int fi(int x){
    if(par[x]==x)
        return x;
    else
        return par[x]=fi(par[x]);
}

void unite(int x,int y){
    x=fi(x),y=fi(y);
    if(x==y)
        return;
    else if(r[x]<r[y]){
        par[x]=y;
    }else{
        par[y]=x;
        if(r[x]==r[y])
            r[x]++;
    }
}

bool same(int x,int y){
    return fi(x)==fi(y);
}

int fa[MAXN];
int son[MAXN];
struct edge{
    int u,v,w;
};
struct es{
    int to,cost;
};
vector <es> G[MAXN];
edge e[MAXM];
long long n,m;
void init(){
    for(int i=0;i<=n;i++)
    {
        par[i]=i;
        r[i]=1;
        G[i].clear();
    }
    memset(fa,0,sizeof(fa));
    memset(son,0,sizeof(son));
}

int cmp(edge a,edge b){
    return a.w<b.w;
}

long long kruskal(){
    sort(e,e+m,cmp);
    long long res=0;
    for(int i=0;i<m;i++){
        edge temp=e[i];
        if(!same(temp.u,temp.v)){
            unite(temp.u,temp.v);
            res+=temp.w;
            es x,y;
            x.to=temp.v;
            x.cost=temp.w;
            y.to=temp.u;
            y.cost=temp.w;
            G[temp.u].push_back(x);
            G[temp.v].push_back(y);
        }
    }
    return res;
}

double ans=0;
double down;
void dfs(int u,int pre){
    son[u]=1;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i].to;
        long long w=G[u][i].cost;
        if(v!=pre){
            dfs(v,u);
            son[u]+=son[v];
        }
    }
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i].to,w=G[u][i].cost;
        if(v!=pre){
            ans+=(w*(long long)(n-son[v])* (long long)(son[v]))/down;
        }
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%I64d%I64d",&n,&m);
        init();
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
        }
        printf("%I64d ",kruskal());
        ans=0;
        down=(n)*(n-1)/2;
        dfs(1,0);
        double res=ans;
        printf("%.2f\n",res);
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值