首页 > 代码库 > UVA 10369(求第K长的边,kruskal算法模板)

UVA 10369(求第K长的边,kruskal算法模板)



 Arctic Networks

Time Limit: 3000 MS

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.

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). 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



题目大意:

有P个站点和S个卫星及无限长度的无线网,要把站点联通,卫星对两地的距离没有任何要求,无线网在距离越长时,所消耗的费用越多,耗费的价格按照那条最长的用无线网架设的路的长度来算,求最小话费。

解题思路:

               kruskal算法模板,边按照从大到小排列,第P-S条即为所求的D。


代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<string>
#define MAXD 510

using namespace std;

int p[MAXD], r[MAXD * MAXD], u[MAXD * MAXD], v[MAXD * MAXD];
double x[MAXD], y[MAXD], w[MAXD * MAXD], ans;
int N, S, P, t, nx, ny;

int find_set( int x)
{
    return p[x] == x ? x : ( p[x] = find_set( p[x]));
}

int cmp( const void *_p, const void *_q)//从大到小排。
{
    int *p = (int *)_p;
    int *q = (int *)_q;
    return w[*p] > w[*q] ? 1 : - 1;
}

void init()
{
    scanf( "%d%d", &S, &P);
    for( int i = 0; i < P; i ++)
        scanf( "%lf%lf", &x[i], &y[i]);
    t = 0;
    for( int i = 0; i < P; i ++)
        for( int j = i + 1; j < P; j ++)
        {
            u[t] = i;
            v[t] = j;
            w[t++]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        }
}

void kruskal()//结合并查集
{
    ans = 0.0;
    for( int i = 0; i < P; i ++)
        p[i] = i;
    for( int i = 0; i < t; i ++)
        r[i] = i;
    qsort( r, t, sizeof (r[0]), cmp);
    int cnt = 0;
    for( int i = 0; i < t; i ++)
    {
        int e = r[i];
        nx = find_set( u[e]);
        ny = find_set( v[e]);
        if( nx != ny) {
            ans = w[e];
            p[nx] = ny;
            cnt ++;
        }
        if( cnt == P - S) break;
    }
}

int main()
{
    scanf( "%d", &N);
    while( N --)
    {
        init();
        kruskal();
        printf( "%.2f\n", ans);
    }
    return 0;
}


UVA 10369(求第K长的边,kruskal算法模板)