POJ 2349Arctic Network【最小生成树】

来源:互联网 发布:java分布式调度框架 编辑:程序博客网 时间:2024/05/15 21:43

Arctic Network
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 16535 Accepted: 5264

Description

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

12 40 1000 3000 600150 750

Sample Output

212.13

Source

Waterloo local 2002.09.28

这题怎么回事???我用G++交死活A不了又找不出bug,最后换了C++马上A了……

很明显的最小生成树。让求的是第s+1边。


#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>using namespace std;int per[555];double e[260000];struct Point{double x,y;}po[555];struct Edge{int s,e;double w;}a[260000];double Math(double x1,double y1,double x2,double y2){double sum=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));return sum;}int Find(int r){while(r!=per[r])r=per[r];return r;}int Judge(int x,int y){int fx=Find(x);int fy=Find(y);if(fx!=fy){per[fx]=fy;return 1;}return 0;}int cmp(Edge a,Edge b){if(a.w<b.w)return 1;return 0;}int main(){int n;int s,p;int i,j;scanf("%d",&n);while(n--){scanf("%d%d",&s,&p);for(i=1;i<=p;i++){per[i]=i;scanf("%lf%lf",&po[i].x,&po[i].y);}int k=0;for(i=1;i<=p;i++){for(j=i+1;j<=p;j++){a[k].s=i;a[k].e=j;a[k].w=Math(po[i].x,po[i].y,po[j].x,po[j].y);k++;}}sort(a,a+k,cmp);for(i=0,j=0;i<k;i++){if(Judge(a[i].s,a[i].e)){e[j]=a[i].w;j++;}}//sort(e,e+j); 找bug的时候我发现这句加不加没有影响 printf("%.2lf\n",e[j-s]);}return 0;}

0 0
原创粉丝点击