poj2349Arctic Network

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 const int maxn=550;
 7 int n,m;
 8 int f[maxn];
 9 double ans[maxn];
10 struct node
11 {
12     int x,y;
13 }p[maxn];
14 struct edge
15 {
16     int u,v;
17     double w;
18     bool operator<(const edge& a)
19     {
20         return w<a.w;
21     }
22 }e[maxn*maxn];
23 
24 void init()
25 {
26     for(int i=0;i<=n;i++)
27         f[i]=i;
28 }
29 
30 int gf(int x)
31 {
32     return x==f[x]?x:f[x]=gf(f[x]);
33 }
34 
35 void uni(int a,int b)
36 {
37     int pa=gf(a);
38     int pb=gf(b);
39     f[pb]=pa;
40 }
41 double getdis(int i,int j)
42 {
43     double dx=p[i].x-p[j].x;
44     double dy=p[i].y-p[j].y;
45     return sqrt(dx*dx+dy*dy);
46 }
47 
48 int main()
49 {
50     int t;
51     scanf("%d",&t);
52     while(t--)
53     {
54         scanf("%d%d",&m,&n);
55          init();
56         for(int i=0;i<n;i++)
57             scanf("%d%d",&p[i].x,&p[i].y);
58         int cnt=0,cas=0;
59         for(int i=0;i<n;i++)
60             for(int j=i+1;j<n;j++)
61         {
62             e[cnt].u=i;
63             e[cnt].v=j;
64             e[cnt].w=getdis(i,j);
65             cnt++;
66         }
67         if(m>=n) {printf("0.00\n");continue;}
68         sort(e,e+cnt);
69         for(int i=0;i<cnt;i++)
70         {
71             int v=e[i].v;
72             int u=e[i].u;
73             if(gf(u)!=gf(v))
74                 {
75                 uni(u,v);
76                 cas++;
77                 ans[cas]=e[i].w;
78                 if(cas==n-1) break;
79                 }
80         }
81         printf("%.2f\n",ans[n-m]);
82 
83 
84     }
85 }

 

posted @ 2017-03-25 01:13  yijiull  阅读(72)  评论(0编辑  收藏  举报