码迷,mamicode.com
首页 > Web开发 > 详细

POJ 2349 Arctic Network

时间:2017-01-24 22:59:23      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:eve   des   exce   bre   int   return   desc   dia   enter   

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

1
2 4
0 100
0 300
0 600
150 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

标签:eve   des   exce   bre   int   return   desc   dia   enter   

原文地址:http://www.cnblogs.com/suishiguang/p/6347949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!