G

在北极圈有一些基地,这些基地需要建立通讯网络,他们可以通过卫星直接通信或者无线通信,基地有S个卫星,N个基地,不过无线通信的传输距离是D,距离越远费用越高,现在想知道D最小是多少。

分析:使用krusal添加p-s条边就行了,因为剩下的边肯定会比已经添加的边要长,在添加的边里面选取最长的那条,也就是最后添加的那条。

************************************************************************************
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;

#define maxn 505

int f[maxn];
struct node
{
    int u, v;
    double len;
    friend bool operator < (node a, node b)
    {
        return a.len > b.len;
    }
};
struct point{double x, y;}p[maxn];
double Len(point a, point b)
{
    double x = a.x - b.x;
    double y = a.y - b.y;
    double l = sqrt(x*x+y*y);

    return l;
}
int Find(int x)
{
    if(f[x] != x)
        f[x] = Find(f[x]);
    return f[x];
}

int main()
{
    int T;

    scanf("%d", &T);

    while(T--)
    {
        int S, N, u, v;
        node s;
        priority_queue<node> Q;

        scanf("%d%d", &S, &N);

        for(s.u=1; s.u<=N; s.u++)
        {
            f[s.u] = s.u;
            scanf("%lf%lf", &p[s.u].x, &p[s.u].y);
            for(s.v=1; s.v<s.u; s.v++)
            {
                s.len = Len(p[s.u], p[s.v]);
                Q.push(s);
            }
        }

        while(Q.size() && S < N)
        {
            s = Q.top();Q.pop();
            u = Find(s.u), v = Find(s.v);

            if(u != v)
            {
                S++;
                f[u] = v;
            }
        }

        printf("%.2f ", s.len);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liuxin13/p/4675235.html