UVa - 10369 - Arctic Network ( 最小生成树 Kruscal )

来源:互联网 发布:linux path是什么 编辑:程序博客网 时间:2024/06/05 10:11

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

12 40 1000 3000 600150 750

Sample Output

212.13

题目大意:国防部要让 p 个地方实现通信。实现通信的方式有2种,第一种是在该地方连接卫星(可以实现任意距离的通信),第二种是在两地之间建立无线电通信,建造成本和距离D成正比。现在有 s 个卫星,如果要实现 p 个地方的通信(直接或间接)。让D尽可能的小,求出最大的D。题目输入给出每个地方的坐标。

题目思路:构图后求出MST,那么较大的边尽可能都用卫星通信,剩下的最大的边就是要求的D。


#include<cstdio>#include<algorithm>#include<cmath>#define N 505using namespace std;double x[N],y[N];int f[N];int num,cnt,s,p;struct  E{int a,b;double v;}e[N*N];bool cmp(E e1,E e2){return e1.v<e2.v;}int find(int x){return f[x] == x ? x : f[x] = find(f[x]);}void Kruscal(){sort(e,e+num,cmp);for(int i=0 ;i<num ;i++){int fa = find(e[i].a);int fb = find(e[i].b);if(fa!=fb){f[fa] = fb;cnt++;}if(cnt == p-s){printf("%.2f\n",e[i].v);break;}}}void init(){scanf("%d%d",&s,&p);for(int i=0 ;i<N ;i++){f[i] = i;}for(int i=0 ;i<p ;i++){scanf("%lf",&x[i]);scanf("%lf",&y[i]);}for(int i=0 ;i<p ;i++){for(int j=i+1 ;j<p ;j++){e[num].a = i;e[num].b = j;e[num++].v = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));}}}int t;int main(){scanf("%d",&t);while(t--){num = 0;cnt = 0;init();Kruscal();}return 0;}



1 0