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

来源:互联网 发布:php集成环境包 编辑:程序博客网 时间:2024/06/07 16:59

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

14 61 2 12 3 23 4 34 1 41 3 52 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
原创粉丝点击