标签:des style http color os io strong for
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 10211 | Accepted: 3374 |
Description
Input
Output
Sample Input
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
Source
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=500+10;
int s,p,root[maxn],cal;
double dis[maxn*maxn];
struct Point
{
int x,y;
}point[maxn];
struct Edge
{
int u,v;
double distance;
}edge[maxn*maxn];
int findroot(int x)
{
if(root[x]!=x)
root[x]=findroot(root[x]);
return root[x];
}
double caldistance(Point a,Point b)
{
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
}
bool cmp(Edge a,Edge b)
{
return a.distance<b.distance;
}
bool cmp2(double a,double b)
{
return a>b;
}
void kruscal()
{
memset(dis,0.0,sizeof(dis));
int sum=0,i,k=0;
for(i=1;i<=cal;i++)
{
int fx=findroot(edge[i].u);
int fy=findroot(edge[i].v);
if(fx==fy)
continue;
else
{
root[fx]=fy;
sum++;
dis[sum]=edge[i].distance;
if(sum==p-1)
break;
}
}
sort(dis+1,dis+1+sum,cmp2);
printf("%.2f\n",dis[s]);
}
void read_graph()
{
for(int i=1;i<=p;i++)
root[i]=i;
for(int i=1;i<=p;i++)
scanf("%d%d",&point[i].x,&point[i].y);
for(int i=1;i<p;i++)
for(int j=i+1;j<=p;+j++)
{
double temp=caldistance(point[i],point[j]);
edge[++cal].u=i;
edge[cal].v=j;
edge[cal].distance=temp;
}
sort(edge+1,edge+1+cal,cmp);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cal=0;
scanf("%d%d",&s,&p);
read_graph();
kruscal();
}
return 0;
}
poj2349Arctic Network(最小生成树kruscal+第k长的边),布布扣,bubuko.com
poj2349Arctic Network(最小生成树kruscal+第k长的边)
标签:des style http color os io strong for
原文地址:http://blog.csdn.net/u014303647/article/details/38487123