Eddy's picture

                                                  Eddy's picture

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 3
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
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int pre[101];
void itin()
{
    for(int i=0;i<101;i++)
    pre[i]=i;
}
struct point
{
    float x,y;
}p[100];
struct dis
{
    int u,v;
    float val;
}d[100010];
int cmp(dis n1,dis n2)
{
    if(n1.val<n2.val)
    return 1;
    return 0;
}
int find(int x)
{
    while(x!=pre[x])
    x=pre[x];
    return x;
}
bool join(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
        return true;
    }
    return false;
}
int main()
{
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        itin();
        int i,j;
        memset(p,0,sizeof(p));
        float sum=0;
        for(i=0;i<t;i++)
        scanf("%f%f",&p[i].x,&p[i].y);
        int cnt=0;
        for(i=0;i<t;i++)
        for(j=i+1;j<t;j++)
        {
            float dist=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
            d[cnt].u=i;
            d[cnt].v=j;
            d[cnt++].val=dist;
        }
        sort(d,d+cnt,cmp);
        /*for(i=0;i<cnt;i++)
        printf("%f ",d[i].val);
        printf("
");*/
        for(i=0;i<cnt;i++)
        {
            if(join(d[i].u,d[i].v))
            {
            sum+=d[i].val;
            //printf("%f
",sum);
            }
            
        }
        printf("%.2f
",sum);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/playboy307/p/5273838.html