题意:
小青蛙(A)找女朋友(B)。途中有好多石头。你就跳吧。。。因此A到B有很多个路径。找这些路径中最短的一条。并把这条路径中两个石头距离最大的那段输出。
题解:
kruskal就行
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int pre[210];
struct point1
{
int in,out;
}t[210];
struct point2
{
int s,e;
double leth;
}eg[150000];
int n;
bool cmp(point2 a,point2 b)
{
return a.leth<b.leth;
}
int find(int w)
{
int now,root=w;
while(root!=pre[root])
root=pre[root];
while(root!=pre[w])
{
now=pre[w];
pre[w]=root;
w=now;
}
return root;
}
int main()
{
int cas=1;
while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&t[i].in,&t[i].out);
}
for(int i=0;i<n;i++)
pre[i]=i;
int k=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
eg[k].s=i;
eg[k].e=j;
eg[k++].leth=sqrt((double)((t[i].in-t[j].in)*(t[i].in-t[j].in)+(t[i].out-t[j].out)*(t[i].out-t[j].out)));
}
int kk=0;
double sum;
sort(eg,eg+k,cmp);
for(int i=0;i<k;i++)
{
int x,y;
if(find(0)!=find(1)) //看看A和B连通了么
{
sum=eg[i].leth;
x=find(eg[i].s);y=find(eg[i].e);
if(x!=y)
pre[y]=x;
}
else
break;
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n",cas++,sum);
}
return 0;
}