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

题意:求生成树的第n-s小边
代码:

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include  <cassert>
#include   <cstdio>
#include   <vector>
#include   <string>
#include    <cmath>
#include    <queue>
#include    <stack>
#include      <set>
#include      <map>
using namespace std;
#define P(a,b,c) make_pair(a,make_pair(b,c))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef pair<int,pair<int,int> >pii;
typedef long long ll;
const ll mod = 1000000007;
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

const int maxn=1e4+5;
const int maxm=1e6+5;
int F[maxn];
int tol;
int find(int x){
	return F[x]==-1?x:F[x]=find(F[x]);
}
struct Edge{
	int u;
	int v;
	double w;
}edge[maxm];

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

bool cmp(Edge &a, Edge &b){
	return a.w<b.w;
}

double Kruskal(int n,int s)//传入点数,返回最小生成树的权值,如果不连通返回-1 
{
    memset(F,-1,sizeof(F));  
    sort(edge,edge+tol,cmp);  
    int cnt=0;//计算加入的边数  
    double ans=0;  
    for(int i=0;i<tol;i++)  {
        int u=edge[i].u;   
        int v=edge[i].v;   
        double w=edge[i].w;   
        int t1=find(u);   
        int t2=find(v);   
        if(t1!=t2)   {    
            ans=w;    
            F[t1]=t2;    
            cnt++;   
        }   
        if(cnt==n-s)break;  
    }  
    if(cnt<n-s)return -1;//不连通  
    else return ans; 
}
pair<double,double>p[1014];
double dist(int i,int j){
	return sqrt((p[i].first-p[j].first)*(p[i].first-p[j].first)+(p[i].second-p[j].second)*(p[i].second-p[j].second));
}
int main(){
	int t,s,n;
	scanf("%d",&t);
	while(t--){
		tol=0;
		scanf("%d%d", &s,&n);
		rep(i,1,n)scanf("%lf%lf",&p[i].first,&p[i].second);
		rep(i,1,n)
			rep(j,1,n){
				if(j>i)addedge(i,j,dist(i,j));
			}
		printf("%.2f", Kruskal(n,s));
		if(t)printf("\n"); 
	}
	return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注