最小生成树--kruskal算法+任意两点间距离之和

Abandoned country

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

 

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

#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>

using namespace std;

const int MAX = 1000005;
int n,m;
int sum[MAX];
long Min_Tree_Len;
long Result;

struct TNode{
    int v,w;
    TNode(){}
    TNode(int v,int w){
        this->v = v;
        this->w = w;
    }
};
vector<TNode> G[MAX];

struct edge{
    int u,v;
    int w;
}e[MAX];

int Parent[MAX];

bool Cmp(const edge& a,const edge& b){
    return a.w < b.w;
}

int find(int x)
{
    while(x != Parent[x]){
        x = Parent[x];
    }
    return x;
}


void sourse(int u,int v,int w)
{
    u = find(u);
    v = find(v);
    if(u != v){
        G[u].push_back(TNode(v,w));
        G[v].push_back(TNode(u,w));
        Min_Tree_Len += w;
        Parent[u] = v;
    }
}

void dfs(int Cur,int Father){
    sum[Cur] = 1;
    for(int i = 0;i < G[Cur].size();i++){
        TNode e = G[Cur][i];
        int son = e.v;
        if(son == Father){
            continue;
        }
        dfs(son,Cur);
        sum[Cur] += sum[son];
        Result += (sum[son] * (n - sum[son])) * e.w;
    }
}

void Init(){
    Result = 0;
    memset(sum,0,sizeof(sum));
    for(int i = 1;i <= n;i ++){
        G[i].clear();
    }
    for(int i = 1;i <= n;i ++){
        Parent[i] = i;
    }
    for(int i = 0;i < m;i ++){
        sourse(e[i].u,e[i].v,e[i].w);
    }
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i = 0;i < m;i ++){
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
        }
        sort(e,e + m,Cmp);
        Init();
        dfs(1,-1);
        int s = n * (n - 1) / 2;
        printf("%d %.2f\n",Min_Tree_Len,(double)Result / s);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值