HDUoj 5723 Abandoned country(最小生成树+dfs

Abandoned country

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

Hint

题意

给我们n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少 并且求每条路的花费期望

题解:

明显的先最小生成树 然后用vector把生成的树存下来 dfs跑每个结点点连接的另外几个点 (总的结点-直接的结点)* 这个边的权值 就是这条边的总的权值 将总的权值加起来/总的路径n*(n-1) 就是每个边 的最小期望和了啊 这个地方纠结半天TAT

AC代码

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a));

const int N = 1e5+10;
const int M = 1e6+10;
struct node {
    int a, b, c;
}edge[M];
vector<pair<int,int> > v[N];
int  par[N], vis[N];
int n, m;
LL ans;
bool cmp(node x, node y)
{
    return x.c < y.c;
}
void init()
{
    for(int i = 0; i<= n; i++) {
        par[i] = i;
    }
}
int find(int x)
{
    if(x == par[x]) return x;
    return par[x] = find(par[x]);
}
LL dfs(int x)
{
    vis[x] = 1;
    LL s, t;            // t 所有与当前节点相连的节点数 
    s = t = 0;          // s  当前节点直接连接得节点数 
    for(int i = 0; i < v[x].size(); i++) {
        int y = v[x][i].first;
        if(!vis[y]) {
            s = dfs(y);
            t += s;
            ans += s*(n-s)*v[x][i].second;
        }
    }
    return t+1;
}
int main()
{
    //ios::sync_with_stdio(false);
    int T;
    scanf("%d",&T);
    while(T--) {
        for(int i = 0; i<= N; i++) v[i].clear();
        CLR(vis,0);
        ans = 0;
        LL sum = 0;
        int res, k = 0;
        scanf("%d%d",&n,&m);
        if(n==0 || m==0) {
            printf("0 0.00\n");
            continue;
        }
        init();
        for(int i = 0;i < m; i++) {
            scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].c);
        }
        sort(edge,edge+m,cmp);
        for(int i = 0;i < m; i++) {
            int x = find(edge[i].a);
            int y = find(edge[i].b);
            if(x != y) {
                k++;
                par[x] = y;
                sum += edge[i].c;
                v[edge[i].a].push_back(make_pair(edge[i].b,edge[i].c));
                v[edge[i].b].push_back(make_pair(edge[i].a,edge[i].c));
            }
            if(k == n-1) break;
        }
        for(int i = 1;i <= n; i++) {
            if(v[i].size() == 1) {     //从一棵树的顶点开始跑 
                res = i;
                break;
            }
        }
        LL p = dfs(res);
        double val = 1.0*n*(n-1)/2;
        printf("%lld %.2lf\n",sum,(double)ans*1.0/val);
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值