WOJ 656 最小生成树 Prim

来源:互联网 发布:gif制作 知乎 编辑:程序博客网 时间:2024/06/09 19:38

656. Wifi Relay

Time Limit: 2 second

The ACM/ICPC team has bought nnn wifi APs. And the APs have a special feature called Wifi Relay which means if two APs' signal can cover each other, then they can work like a single wifi AP.

Each AP can cover a round area, the radius of which is rrr.

In a programming contest, GSS set up nnn APs, and the iii-th AP is placed at (xi,yi)(x_i, y_i)(xi,yi). Then GSS use the Wifi Relay to let all the APs work like a single AP that every one can access the system without being interrupted.

Then GSS has to set all the radius each APs can cover to rrr. To save pover, GSS has to get the minimal rrr he should set, can you help him to do that?

Input

The first line of input is an integer nnn. (2≤n≤1042 \le n \le 10^42n104)

The following nnn lines, the iii-th one of them contains two integers (xi,yi)(x_i, y_i)(xi,yi), the position GSS place iii-th AP. (0≤xi,yi≤1080\le x_i, y_i \le 10^80xi,yi108)

Output

Output one integer, the minimal rrr GSS should set. Your answer will be considered as correct if the relative or absolute error is less than10−910^{-9}109

Examples

Input 1

30 010 1010 0

Output 1

5.0000000000

Source

第六届华中区程序设计邀请赛暨武汉大学第十五届校赛



        如此水题当时竟然没有做出来?罪过啊。

        其实一切都是因为没有信心写。看着O(N^2)的复杂度,根本不敢下手……其实据说O(N^2logN)的算法都可以解……

        具体说说吧,最小生成树中最长的边即为本题的解。下面给出自己简短的证明。假设存在一个解x,比最小生成树的最长边y要短,那么就说明能够以长度小于等于x的边构成一颗生成树。根据kruskal最小生成树的求解法,如果用长度小于等于x的边已经能够构成最小生成树了,那么边y>x就不是最小生成树中的边,与题设矛盾,所以这个最长边y就是本题的解。

        然后考虑到本题是一个稠密图,所以用Prim求解最小生成树更为快速。具体代码:

#include<bits/stdc++.h>#define INF 1e17using namespace std;struct node{int x,y;} wifi[10010];inline long long sqr(long long x) {return x*x;}inline long long dist(node a,node b){return sqr(a.x-b.x)+sqr(a.y-b.y);}long long d[10010],n;bool v[10110];int main(){cin>>n;for(int i=1;i<=n;i++){scanf("%d%d",&wifi[i].x,&wifi[i].y);d[i]=INF;}v[1]=1; int last=1;long long ans=0;for(int i=1;i<n;i++){int k=0;long long cur=INF;for(int j=1;j<=n;j++){if (v[j]) continue;d[j]=min(d[j],dist(wifi[j],wifi[last]));if (d[j]<cur) k=j,cur=d[j];}ans=max(ans,d[k]);v[k]=1; last=k;}cout << fixed << setprecision(12) << sqrt(ans) / 2 << endl;return 0;}

0 0