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
题目描述:
分析:
题目本质上是最小生成树问题。我们不妨把2个哨塔间直接通讯看出一条边。由于有s个卫星,那么有s-1条边是可以无视距离的。又因为最小生成树是有p-1条边,那么只要无视最大的s-1条边的距离(使用卫星),那么剩下了最长的边就是满足题目的最小的D了。
kruskal算法是从小到大开始选边的,只要选到第p-1-(s-1)条边(无视距离外的最长的边),就是D了。
代码:
#include#include #include #include using namespace std; struct point { int x; int y; }p[506]; struct edge { int u; int v; double d; }e[250006]; int parent[506]; int Rank[506]; int find(int x) { if(x==parent[x]) return x; return parent[x]=find(parent[x]); } void Union(int x,int y) { int xr=find(x); int yr=find(y); if(xr!=yr) parent[xr]=yr; } bool tong(int x,int y) { return find(x)==find(y); } double cald(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y)*1.0); } double cmp(edge a,edge b) { return a.d<b.d; } int main() { int T; scanf("%d",&T); while(T--) { int s,c; double ans=0; scanf("%d%d",&s,&c); for(int i=1;i<=c;i++) { scanf("%d%d",&p[i].x,&p[i].y); parent[i]=i; } int posi=0; for(int i=1;i ) { for(int j=i+1;j<=c;j++) { e[posi].u=i; e[posi].v=j; e[posi++].d=cald(p[i],p[j]); } } sort(e,e+posi,cmp); int line=0; int end=c-1-(s-1); for(int i=0;i ) { if(!tong(e[i].u,e[i].v)) { Union(e[i].u,e[i].v); line++; } if(line==end) { ans=e[i].d; break; } } printf("%.2f\n",ans); } return 0; }