Kruskal第一次连接的时候就是最小值
#include<bits/stdc++.h>
using namespace std;
const int maxn=210;
int pra[maxn],rank1[maxn];
int n,num;
struct point{int x,y;}p[maxn];
struct node
{
int u,v;
double dis;
bool operator<(const node &a)const
{
return dis<a.dis;
}
}edge[40010];
void init()
{
num=0;
for(int i=0;i<=n;i++)
pra[i]=i,rank1[i]=0;
}
double cal(int a,int b)
{
return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y));
}
void add_edge(int u,int v,double d)
{
edge[num].u=u;
edge[num].v=v;
edge[num++].dis=d;
}
void process(int m)
{
for(int i=0;i<m;i++)
{
double d=cal(i,m);
add_edge(i,m,d);
}
}
int find1(int x)
{
if(x==pra[x])return x;
return pra[x]=find1(pra[x]);
}
void unite(int x,int y)
{
if(rank1[x]<rank1[y])
pra[x]=y;
else
{
pra[y]=x;
if(rank1[x]==rank1[y])rank1[x]++;
}
}
int main()
{
//freopen("in.txt","r",stdin);
int cas=1;
while(scanf("%d",&n)!=EOF,n)
{
init();
for(int i=0;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
process(i);
}
sort(edge,edge+num);
printf("Scenario #%d\n",cas++);
for(int i=0;i<num;i++)
{
int x=find1(edge[i].u);
int y=find1(edge[i].v);
if(x!=y)unite(x,y);
if(find1(0)==find1(1))
{
printf("Frog Distance = %.3f\n",edge[i].dis);
break;
}
}
printf("\n");
}
return 0;
}