Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25165   Accepted: 7751

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
题意:国防部(DND)要用无线网络连接北部几个哨所。有两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道。 
   任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置。同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率。功率越大,D也越大,但成本更高。出于采购和维修的方便,所有哨所的收发器必须是相同的;也就是说,D值对每一个哨所相同。 
你的任务是确定收发器的D的最小值。每对哨所间至少要有一条通信线路(直接或间接)。
 
思路:有 m 个哨所需要m-1条边连接,s颗卫星可以代替s-1条边,用卫星代替最长的边
  1 #include <iostream>
  2 #include <cmath>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cstdio>
  6 using namespace std;
  7 #define N 510
  8 
  9 const double M=1e12;//定义一个大值  
 10 int p,s;
 11 
 12 struct dot //哨站结点 
 13 {
 14     double x,y;
 15 }d[N];
 16 
 17 double map[N][N];
 18 double rank[N],dst[N];//rank放最小生成树的各边长,dst放各点到MST的最近距离 
 19 int vis[N];
 20 
 21 double dist(int i,int j)//求两点距离 
 22 {
 23     double x,y;
 24     x=d[i].x-d[j].x;
 25     y=d[i].y-d[j].y;
 26     
 27     return sqrt(x*x+y*y);
 28 }
 29 
 30 void init()
 31 {
 32     int i,j;
 33     
 34     memset(map,0,sizeof(M));
 35     for (i=0;i<p;i++)//初始化图 
 36     {
 37         for (j=0;j<p;j++)
 38         {
 39             if (i==j)
 40             {
 41                 map[i][j]=0;
 42             }
 43             
 44             else
 45             {
 46                 map[i][j]=map[j][i]=dist(i,j);
 47             }
 48         }
 49     }
 50     
 51     memset(vis,0,sizeof(vis));
 52     memset(dst,0,sizeof(dst));
 53 }
 54 
 55 //bool cmp(int i,int j)
 56 //{
 57 //    if (rank[i]<rank[j])
 58 //    {
 59 //        return true;
 60 //    }
 61 //    return false;
 62 //}
 63 
 64 void findans()
 65 {
 66     int i,j;
 67     sort(rank,rank+p-1);//按增序排列,注意排序范围为rank+p-1,因为MST只有p-1条边 
 68     printf("%.2fn",rank[p-s-1]);// (p-1)-(s-1)-1,因为序号从0开始 
 69     
 70 //    for (i=0;i<p;i++)
 71 //    {
 72 //        for (j=0;j<p;j++)
 73 //        {
 74 //            printf("%10.2f",map[i][j]);
 75 //        }
 76 //        printf("n");
 77 //    }
 78 //    printf("n");
 79 //    for (i=0;i<p-1;i++)
 80 //    {
 81 //        printf("%10.2f",rank[i]);
 82 //    }
 83 }
 84 
 85 void prime()
 86 {
 87     int cnt=0,k,j,point,i;
 88     double min;
 89     
 90     vis[0]=1;//把0点放入MST 
 91     for (i=0;i<p;i++)//初始化dst 
 92     {
 93         dst[i]=map[i][0];
 94     }
 95     
 96     for (i=1;i<p;i++)//找距MST最近的点 
 97     {
 98         min=M;
 99         for (j=0;j<p;j++)
100         {
101             if (vis[j]==0&&min>dst[j])
102             {
103                 min=dst[j];
104                 point=j;
105             }
106         }
107         
108 //        if (min==M)
109 //        {
110 //            break;
111 //        }
112         
113         vis[point]=1;
114         rank[cnt++]=min;
115         
116         for (k=0;k<p;k++)//更新各点到MST的最小距离 
117         {
118             if (vis[k]==0&&dst[k]>map[k][point])
119             {
120                 dst[k]=map[k][point];
121             }
122         }
123     }
124     findans();
125 }
126 
127 int main()
128 {
129     int i,n,j;
130     double x,y;
131     
132     scanf("%d",&n);
133     for (i=0;i<n;i++)
134     {
135         scanf("%d%d",&s,&p);
136         
137         for (j=0;j<p;j++)
138         {
139             scanf("%lf%lf",&d[j].x,&d[j].y);
140         }
141         
142         init();
143         prime();
144     }
145     
146     return 0;
147 } 

 

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!