10034 - Freckles(最小生成树)

注意输出格式

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = (55 * 99 + 10)*2;
int p[N];
struct Line
{
    int s, e;
    double len;
}q[N];
double x[N], y[N];
double dis(double x1, double y1, double x2, double y2)
{
    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
int cmp(Line a, Line b)
{
    return a.len < b.len;
}
int find(int x)
{
    return x == p[x]? x:find(p[x]);
}
int main(void)
{
    int kase;
    scanf("%d", &kase);
    while(kase--)
    {
        int n, sum = 0;
        scanf("%d", &n);
        for(int i = 0;i < n;i++)
        {
            scanf("%lf %lf", &x[i], &y[i]);
            for(int j = 0;j < i;j++)
            {
                double len = dis(x[i], y[i], x[j], y[j]);
                q[sum++] = {i, j, len};
            }
        }
        sort(q, q+sum, cmp);
        for(int i = 0;i <= sum;i++)
            p[i] = i;
        double ans = 0.0;
        for(int i = 0;i < sum;i++)
        {
            int fa = find(q[i].s);
            int fb = find(q[i].e);
            if(fa != fb)
            {
                p[fa] = fb;
                ans += q[i].len;
            }
        }
        printf("%.2lf\n", ans);
        if(kase != 0)
            printf("\n");
    }
    return 0;
}


你可能感兴趣的:(10034 - Freckles(最小生成树))