Eddy's picture
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2490
Accepted Submission(s): 1197
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
Author
eddy
Recommend
JGShining
今天简直太顺利了,到现在竟然都是直接1AC,Yeah!!
题意就是有N个点,然后连接这些点,找出最短能够把所有点连接起来的折线的长度,最小生成树问题
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
struct point{
int begin,end;
double len;
}map[10000];
double x[108],y[108];
int flag[108];
int cmp(const void *a,const void *b)
{
struct point *c,*d;
c=(struct point *)a;
d=(struct point *)b;
return c->len >d->len?1:-1;
}
int father(int x)
{
if(x==flag[x])
return x;
flag[x]=father(flag[x]);
return flag[x];
}
double Distance(int i,int j)
{
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
int main()
{
int N,i,j,k,fa,fb;
double sum;
while(scanf("%d",&N)!=EOF)
{
for(i=1;i<=N;i++)
{
scanf("%lf%lf",&x[i],&y[i]);
flag[i]=i;
}
k=0;
for(i=1;i<=N;i++)
for(j=1;j<i;j++)
{
map[k].begin=i;
map[k].end=j;
map[k++].len=Distance(i,j);
}
qsort(map,k,sizeof(map[0]),cmp);
sum=0;
for(i=0;i<k;i++)
{
fa=father(map[i].begin);
fb=father(map[i].end);
if(fa!=fb){
sum+=map[i].len;
if(fa>fb)
flag[fa]=fb;
else flag[fb]=fa;
}
}
printf("%.2lf\n",sum);
}
return 0;
}