圖論 最小生成樹 kruskal
Time Limit: 2000MS | Memory Limit: 65536K |
Total Submissions: 17990 | Accepted: 5723 |
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
已有S個衛星電話可以部署,部署了衛星電話的城市之間通話代價為0
一眼可以看出這是一個最小生成樹問題,那麼當然要利用S,貪心地把生成樹中的S-1條邊消掉。剩下的最長邊就是答案
1 /*by SilverN*/
2 #include<algorithm>
3 #include<iostream>
4 #include<cstring>
5 #include<cstdio>
6 #include<cmath>
7 #include<vector>
8 using namespace std;
9 const int mxn=610;
10 int read(){
11 int x=0,f=1;char ch=getchar();
12 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
13 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
14 return x*f;
15 }
16 struct point{
17 int x,y;
18 }p[mxn];
19 double dist(int a,int b){
20 return sqrt((double)(p[a].x-p[b].x)*(p[a].x-p[b].x)+(double)(p[a].y-p[b].y)*(p[a].y-p[b].y));
21 }
22 struct edge{
23 int x,y;
24 double d;
25 }e[mxn*mxn];
26 int cmp(const edge a,const edge b){
27 return a.d<b.d;
28 }
29 int s,n,cnt;
30 //
31 int fa[mxn];
32 int find(int x){
33 if(fa[x]==x)return x;
34 return fa[x]=find(fa[x]);
35 }
36 void kruskal(int s){
37 for(int i=1;i<=n;++i)fa[i]=i;
38 int num=1;
39 double ans=0;
40 for(int i=1;i<=cnt;++i){
41 int f1=find(e[i].x);
42 int f2=find(e[i].y);
43 if(f1!=f2){
44 fa[f1]=f2;
45 ans=max(ans,e[i].d);
46 num++;
47 }
48 if(num==n-s)break;
49 }
50 printf("%.2f\n",ans);
51 return;
52 }
53 int T;
54 int main(){
55 T=read();
56 int i,j;
57 while(T--){
58 s=read();n=read();--s;
59 for(i=1;i<=n;++i){
60 p[i].x=read();p[i].y=read();
61 }
62 cnt=0;
63 for(i=1;i<n;++i)
64 for(j=i+1;j<=n;++j){
65 e[++cnt].x=i;e[cnt].y=j;
66 e[cnt].d=dist(i,j);
67 }
68 sort(e+1,e+cnt+1,cmp);
69 kruskal(s);
70 }
71 return 0;
72 }
本文為部落客原創文章,轉載請注明出處。