poj 2349 Arctic Network

Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11187   Accepted: 3660

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

 

分析:

Kruskal算法,由短到长排序,生成最小生成树,倒数的s-1条边用卫星通讯代替,由前向后第p-1-(s-1)条边的长度就是所求

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8 struct point{
 9     int x,y;
10 };
11 struct edge{
12     int u,v;
13     double w;
14 };
15 point po[505];
16 int f[505];
17 edge e[250000];
18 bool cmp(edge a,edge b){
19     return a.w<b.w;
20 }
21 int find(int i){//并查集 
22     if(i!=f[i]){
23         f[i]=find(f[i]);
24     }
25     return f[i];
26 }
27 double get_dis(int i,int j){
28     int x=po[i].x-po[j].x;
29     int y=po[i].y-po[j].y;
30     return sqrt(double(x*x+y*y));
31 }
32 double kruskal(int m,int ss){
33     int i=0;
34     for(;i<m;i++){
35         int p=find(e[i].u);
36         int q=find(e[i].v);
37         if(p!=q){
38             if(p>q){
39                 f[q]=p;
40             }
41             else{
42                 f[p]=q;
43             }
44             ss--;
45         }
46         if(ss==0){
47             return e[i].w;
48         }
49     } 
50 }
51 int main(){
52     int t,s,p,m;
53     scanf("%d",&t);
54     while(t--){
55             m=0;
56              scanf("%d %d",&s,&p);    
57              int i,j,k;
58              for(i=0;i<p;i++){
59                      scanf("%d %d",&po[i].x,&po[i].y);
60                      f[i]=i;
61              }
62              for(i=0;i<p;i++){
63                  for(j=i+1;j<p;j++){
64                      e[m].u=i;
65                      e[m].v=j;
66                      double dis=get_dis(i,j);
67                      e[m++].w=dis;
68                      e[m].u=j;
69                      e[m].v=i;
70                      e[m++].w=dis;
71                  }
72              }
73              sort(e,e+m,cmp);
74              /*for(i=0;i<m;i++)
75                  cout<<"i:  "<<e[i].w<<endl;*/
76              printf("%.2f\n",kruskal(m,p-s));
77     }
78     return 0;
79 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。