练习四 1002

来源:互联网 发布:华3交换机端口激活命令 编辑:程序博客网 时间:2024/04/30 12:19

Problem B

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 58   Accepted Submission(s) : 18

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.<br>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?<br>

 


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. <br><br>Input contains multiple test cases. Process to the end of file.<br>

 


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. <br>

 


Sample Input

3 1.0 1.0 2.0 2.0 2.0 4.0 

 


Sample Output

3.41
题意:
计算把所有的点连接起来的最小线段长度。
思路:
简单的最小生成树。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
int p[10010],n,k;
struct lok
{
    double x,y;
}a[10010];
struct point 
{
    int s,e;
    double w;
}b[10010];
bool cmp(point a,point b)
{
    return a.w<b.w;
}
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
double kru()
{
    double sum=0;
    for(int i=0;i<k;i++)
    {
        int c=find(b[i].s),d=find(b[i].e);
        if(c!=d)
        {
            p[c]=d;
            sum+=b[i].w;
        }
    }
    return sum;
}
int main()
{
    while(cin>>n)
    {
        if(n==0) break;
        for(int i=1;i<=n;i++)
            cin>>a[i].x>>a[i].y;
        k=0;
        for(int i=1;i<n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                b[k].s=i;
                b[k].e=j;
                b[k].w=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
                k++;
            }
        }
        for(int i=0;i<k;i++)
            p[i]=i;
        sort(b,b+k,cmp);
        cout<<setiosflags(ios::fixed)<<setprecision(2)<<kru()<<endl;
        //printf("%.2lf\n",kru());
    }
    return 0;
}
0 0