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

POJ 2349 Arctic Network(最小生成树中第s大的边)

时间:2018-05-06 01:28:16      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:spec   def   ISE   span   卡尔   lines   printf   int   clu   

题目链接:http://poj.org/problem?id=2349

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
 1 /*
 2 问题
 3 题目理解起来有一定的难度,理解了就是水题一道。怎么理解呢?就是一遍一遍的读。 
 4 重要的是每个顶点会配备一个无线接收器,其中一些顶点还会配备一个卫星信道。现在给出卫星信道的个数和顶点个
 5 数以及每个顶点的坐标,计算并输出构成最小生成树的第s大的那条边是多少
 6 
 7 解题思路
 8 刚开始读题的时候以为要消去s条边,结果计算样例的结果是200,其实是卫星信道的个数,换句换说每两个卫星信道
 9 才可以消去一条边。 
10 处理每两点之间的边,使用克鲁斯卡尔算法一条一条的加入,直至构成最小生成树,然后数s条边即可。 
11 */ 
12 #include<cstdio>
13 #include<math.h>
14 #include<algorithm>
15 
16 using namespace std;
17 struct NODE{
18     int x,y;
19 }node[1010];
20 
21 struct EDGE{
22     int u,v;
23     double w;
24 }edge[300010];
25 int cmp(struct EDGE a,struct EDGE b){
26     return a.w<b.w;
27 }
28 int f[1010];
29 int merge(int v,int u);
30 int getf(int v);
31 int main()
32 {
33     int t,s,p,i,j,k;
34     scanf("%d",&t);
35     while(t--)
36     {
37         scanf("%d%d",&s,&p);
38         for(i=1;i<=p;i++){
39             scanf("%d%d",&node[i].x,&node[i].y);
40         }
41         
42         k=0;
43         for(i=1;i<=p-1;i++){
44             for(j=i+1;j<=p;j++){
45                 edge[k].u=i;
46                 edge[k].v=j;
47                 edge[k].w=sqrt(1.0*(node[i].x-node[j].x)*(node[i].x-node[j].x) + 
48                 1.0*(node[i].y-node[j].y)*(node[i].y-node[j].y));
49                 //printf("%lf\n",edge[k].w);
50             }
51         }
52         sort(edge,edge+k,cmp);
53         
54         /*for(i=0;i<k;i++)
55             printf("%lf\n",edge[i].w);*/    
56             
57         for(i=1;i<=p;i++)
58             f[i]=i;
59             
60         int cou=0;
61         for(i=0;i<k;i++){
62             if(merge(edge[i].u,edge[i].v)){
63                 //printf("选择了第%d这条边%.2lf\n",i,edge[i].w);
64                 cou++;
65             }
66             if(cou == p-s){
67                 printf("%.2lf\n",edge[i].w);
68                 break;
69             }    
70         }
71     }
72     return 0;    
73 } 
74 
75 int getf(int v)
76 {
77     return f[v]==v?v:f[v]=getf(f[v]);
78 }
79 
80 int merge(int v,int u)
81 {
82     int t1,t2;
83     t1=getf(v);
84     t2=getf(u);
85     if(t1 != t2){
86         f[t2]=t1;
87         return 1;
88     }
89     return 0;
90 }

 

POJ 2349 Arctic Network(最小生成树中第s大的边)

标签:spec   def   ISE   span   卡尔   lines   printf   int   clu   

原文地址:https://www.cnblogs.com/wenzhixin/p/8996836.html

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