Arctic Network POJ - 2349

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

Input

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

Output

For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

Sample Input

1
2 4
0 100
0 300
0 600
150 750

Sample Output

212.13

题意:现有 m 个地点, n 个卫星。  卫星有一个神奇的作用, 作用在于只要某个地方安装有卫星, 不论他们之间距离为多远, 都能够互相接收到彼此间信 号, 那么其他地方需要通过收发器 D 来连接。让你求1 - m 个地方在共安装过 n 个卫星后,我们所需要     的 收发器 D 所需的最大距离是多少?

输出用%lf 然后一直错

printf的%f说明符的确既可以输出float型又可以输出double型。根据“默认参数提升”规则float型会被提升为double型。因此printf()只会看到双精度数。对于scanf,情况就完全不同了,它接受指针,这里没有类似的类型提升。向float存储和向double存储大不一样,因此,scanf区别%f和%lf。      
也就是说输出的时候不管输出的是双精度还是单精度都用%f就没错了,但是输入的时候,输入单精度要用%f而输入双精度要用%lf

#include
#include
#include
#include
#include

using namespace std;

struct node
{
    int u, v;
    double w;
} edge[1110000];

struct dian
{
    int x, y;
} p[111000];

int tol = 0;
void addedge(int u, int v, double w)
{
    edge[tol].u = u;
    edge[tol].v = v;
    edge[tol++].w = w;
}
int f[111000];
int Find(int x)
{
    if(f[x] == x)return x;
    else
    {
        return f[x] = Find(f[x]);
    }
}
int cmp(node a, node b)
{
    return a.w < b.w;
}
void init(int n)
{
    for(int i = 0; i <= n; i++)
    {
        f[i] = i;
    }
}
double s(int n, int k)
{
    int cnt = 0;
    init(n);
    sort(edge, edge + tol, cmp);
    for(int i = 0; i < tol; i++)
    {
        int u = edge[i].u;
        int v = edge[i].v;
        double w = edge[i].w;
        int tx = Find(u);
        int ty = Find(v);
        if(tx != ty)
        {
            f[tx] = ty;
            cnt++;
        }
        if(cnt >= n - k)
        {
            return w;
        }
    }
    return 0;
}
double len(dian a, dian b)
{
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
void in()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, k;
        tol = 0;
        scanf("%d %d", &k, &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d %d", &p[i].x, &p[i].y);
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                double l = len(p[i], p[j]);
                addedge(i, j, l);
            }
        }
        double sum = s(n, k);
        printf("%.2f\n", sum);
    }
}
int main()
{
    in();
    return 0;
}

 

你可能感兴趣的:(并查集和最小生成树)