hdu_1875

/*
 * 并查集+最小生成数
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

#define MAXN            101
#define MAXM            9999
#define INF             0x3f3f3f3f
#define LIMITUP         1000
#define LIMITDOWN       10
struct Edge {
        int u, v;
        double w;
}edge[MAXM];

int dx[MAXN], dy[MAXN], e_cnt, root[MAXN], hash[MAXN];

void insert_arc(int u, int v, double w)
{
        edge[e_cnt].u = u;      edge[e_cnt].v = v;
        edge[e_cnt ++].w = w;
}

int get_root(int x)
{
        if( x == root[x] ) {
                return x;
        }
        return root[x] = get_root(root[x]);
}

int cmp(const struct Edge &a, const struct Edge &b)
{
        return a.w < b.w;
}

int can_connect(int a, int b, double &d)
{
        d = sqrt((double)(dx[a]-dx[b])*(dx[a]-dx[b])+(double)(dy[a]-dy[b])*(dy[a]-dy[b]));
        if( d >= LIMITDOWN && d <= LIMITUP ) {
                return 1;
        }
        return 0;
}

double min_panning_tree(int vertex)
{
        double ans(0.0);
        int rx, ry;
        for(int i = 0; i < vertex; i ++) {
                root[i] = i;
        }
        for(int i = 1; i < vertex; i ++) {
                for(int j = 0; j < e_cnt; j ++) {
                        rx = get_root(edge[j].u); ry = get_root(edge[j].v);
                        if( rx != ry ) {
                                root[rx] = ry;
                                ans += edge[j].w;
                                break;
                        }
                }
        }
        return ans*100.0;
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int cas, n, x, y, rx, ry, ans;
        double d;
        scanf("%d", &cas);
        while( cas -- ) {
                scanf("%d", &n); e_cnt = 0;
                for(int i = 0; i < n; i ++) {
                        scanf("%d %d", &dx[i], &dy[i]);
                        root[i] = i;
                }
                for(int i = 0; i < n; i ++) {
                        for(int j = i+1; j < n; j ++) {
                                if( can_connect(i, j, d) ) {
                                        insert_arc(i, j, d);
                                        rx = get_root(i); ry = get_root(j);
                                        if( rx != ry ) {
                                                root[rx] = ry;
                                        }
                                }
                        }
                }
                ans = 0; memset(hash, 0, sizeof(hash));
                for(int i = 0; i < n; i ++) {
                        root[i] = get_root(i);
                        hash[ root[i] ] ++;
                }
                for(int i = 0; i < n; i ++) {
                        if( hash[i] ) {
                                ans ++;
                        }
                }
                if( 1 != ans ) {
                        printf("oh!\n"); continue;
                }
                sort(edge, edge+e_cnt, cmp);
                printf("%.1lf\n", min_panning_tree(n));

        }
        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值