首页 > 代码库 > POJ 2349 Arctic Network

POJ 2349 Arctic Network

Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18447 Accepted: 5829

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
 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cmath>  6 using namespace std; 7 int T,n,tot,fa[510],x,y; 8 const int INF=0x7fffffff; 9 double rt[250005];10 int mark=0;11 struct dian{12     double x,y;13 }node[510];14 struct Edge{15     int u,v;double w;16 }e[250005];17 void add_edge(int u,int v,double w){18     e[tot].u=u;e[tot].v=v;e[tot].w=w;tot++;19 }20 bool cmp(Edge a,Edge b){21     return a.w<b.w;22 }23 void init(){24     memset(fa,-1,sizeof(fa));25     tot=0;26 }27 int find(int x){28     if(fa[x]==-1) return x;29     else return fa[x]=find(fa[x]);30 }31 void kruskal(int n){32     double ans=0;33     sort(e,e+tot,cmp);34     int cnt=0;35     for(int k=0;k<tot;k++){36         int u=e[k].u,v=e[k].v;37         int rx=find(u),ry=find(v);38         if(rx!=ry){39             rt[mark]=e[k].w;ans+=e[k].w;40             fa[rx]=ry;mark++;cnt++;41         }42         if(cnt == n-1) break;43     } 44     //if(cnt<n-1) return -1;45     //else return ans;46 }47 int main()48 {49     scanf("%d",&T);int s;50     while(T--){51         init();52         scanf("%d%d",&s,&n);53         for(int i=1;i<=n;i++)54           scanf("%lf %lf",&node[i].x,&node[i].y);55         for(int i=1;i<=n;i++){56             double d;57             for(int j=1;j<=i;j++){58                 if(i==j){ d=INF; }59                 else{60                     d=sqrt(1.0*(node[i].x-node[j].x)*61                     (node[i].x-node[j].x)62                     +1.0*(node[i].y-node[j].y)63                     *(node[i].y-node[j].y));  64                 }65                 add_edge(i,j,d);66             }67         }68         kruskal(n);69         printf("%.2f\n",rt[mark-s]);70     }71     return 0;72 } 

搞了一晚上的破题终于AC了

POJ 2349 Arctic Network