比较简单一题,求出最小生成树后,第s条长的线段即可,问题是我特么WA了N次不知道原因,最后找来正确代码单步调试差错,最后发现是因为POJ只能用%f输出!!!!老子居然该死的把这一点忘了,还他妈一直想不出来,真是醉了。
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int maxn=505;
struct Point
{
int x;
int y;
}point[maxn];
struct Edge
{
int from;
int to;
double cost;
int visit;
}edge[maxn*maxn];
int m;
int father[maxn];
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(Edge a,Edge b)
{
return a.cost<b.cost;
}
int find(int x)
{
if(father[x]==x) return x;
father[x]=find(father[x]);
return father[x];
}
bool Union(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)
{
return false;
}
else
{
father[fx]=fy;
}
return true;
}
double kruskal( int n )
{
int i , j = 0 ;
double sum = 0 ;
for( i = 0 ; i < n ; i ++ )
father[i] = i ;
sort( edge , edge +m , cmp ) ;
for( i = 0 ; i < m && j < n ; i ++ )
{
if( Union( edge[i].from , edge[i].to ) )
{
sum += edge[i].cost ;
edge[i].visit = 1 ;
j ++ ;
}
}
return sum ;
}
int main()
{
int t;
int s,p;
int i,j;
scanf("%d",&t);
while(t--)
{
m=0;
scanf("%d%d",&s,&p);
for(i=0;i<p;i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
}
for(i=0;i<p-1;i++)
{
for(j=i+1;j<p;j++)
{
edge[m].from=i;
edge[m].to=j;
edge[m].cost=dis(point[i],point[j]);
edge[m].visit=0;
m++;
}
}
kruskal(p);
int u;
for(i=m-1;i>=0;i--){
if( edge[i].visit )
{
s -- ;
if( s == 0 ) break ;
}
}
printf( "%.2f\n" ,edge[i].cost ) ;
}
return 0;
}