您的位置:首页 > 其它

HDUOJ 1162 Eddy's picture(最小生成树)

2012-04-12 23:33 344 查看


Eddy's picture

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3511 Accepted Submission(s): 1722



Problem Description

Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends
are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.

Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length
which the ink draws?

Input

The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of file.

Output

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.

Sample Input

3
1.0 1.0
2.0 2.0
2.0 4.0


Sample Output

3.41


注意快排对double的实现,由于cmp必须返回int,所以不能return c->len - d->len。

AC code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N=100;
typedef struct coodinate
{
int v,u;//两个点
double len;//v、u间的距离
}Point;
Point p[N*(N-1)];

int cmp(const void *a,const void *b)
{
Point *c=(Point *)a;
Point *d=(Point *)b;
return c->len > d->len ? 1:-1;
}

int main()
{
int i,j,k,t,n,vis
,m,M;
double x
,y
,min;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%lf%lf",&x[i],&y[i]);
}
min=0.0;
for(i=0;i<n;i++) vis[i]=i;
for(i=0,k=0;i<n;i++)
{
for(j=i+1;j<n;j++,k++)
{
p[k].v=i;
p[k].u=j;
p[k].len=pow((pow((x[i]-x[j]),2.0)+pow((y[i]-y[j]),2.0)),0.5);
}
}
qsort(p,k,sizeof(p[0]),cmp);
for(i=0,j=0;j<n-1;i++)
{
if(vis[p[i].v] != vis[p[i].u])
{
min+=p[i].len;
j++;
//合并集合
if(vis[p[i].v]>vis[p[i].u])
{
M=vis[p[i].v];
m=vis[p[i].u];
}
else
{
M=vis[p[i].u];
m=vis[p[i].v];
}
for(k=0;k<n;k++)
{
if(vis[k]==M) vis[k]=m;
}
}
}
printf("%.2lf\n",min);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: