http://www.elijahqi.win/archives/4011
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
求一张图,求这张图的最小生成树,去掉K条最长边,剩下的第K+1长的边.
#include<bits/stdc++.h>
#define N 550
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
struct node{
int x,y;double z;
}data[N*N];
int fa[N],T,x[N],y[N],num,s,n;double dis[N<<1];
inline double dist(int x1,int y1,int x2,int y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
inline int find(int x){
while(x!=fa[x]) x=fa[x]=fa[fa[x]];return x;
}
inline bool cmp(const node &a,const node &b){
return a.z<b.z;
}
int main(){
freopen("c.in","r",stdin);
T=read();
while(T--){
s=read();n=read();num=0;
for (int i=1;i<=n;++i) x[i]=read(),y[i]=read(),fa[i]=i;
for (int i=1;i<=n;++i)
for (int j=i+1;j<=n;++j) data[++num]=(node){i,j,dist(x[i],y[i],x[j],y[j])};
// printf("%d\n",num);
sort(data+1,data+num+1,cmp);int tmp=0;
for (int i=1;i<=num;++i){
int xx=find(data[i].x),yy=find(data[i].y);
double d=data[i].z;
if (xx!=yy) fa[xx]=yy,dis[++tmp]=d;
}
printf("%.2f\n",dis[tmp-s+1]);
}
return 0;
}