Problem B

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
题意:在一个平面上给你几个点,让你求用线段将这几个点连起来,需要的最小长度;
解题思路:这里要想用最短路的话,父亲数组里必须做一个替换,第n个点的父亲数组值对应n,这样才能进行kruskal;
感悟:在电阅一气呵成,0ms过的感觉真是爽啊;
代码:
#include
#define N 110
using namespace std;
struct node
{
    double f,e,w;//点的坐标,距离
};
bool comp(node a,node b)
{
    return a.w
}
node fr[N*N];
int bin[N];

double dis(double a,double b,double c,double d)
{
    return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}
int find1(int x)
{
    int r=x;
    while(bin[r]!=r)
        r=bin[r];
    return r;
}
double kruskal(int n,int m)
{
    sort(fr,fr+m,comp);
    for(int i=0;i<=n;i++)
        bin[i]=i;
    double s=0;
    for(int i=0;i
    {
        int fx=find1(fr[i].f);
        int fy=find1(fr[i].e);
        if(fx!=fy)
        {
            bin[fx]=fy;
            s+=fr[i].w;
        }
    }
    return s;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    double m[N][N];
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%lf%lf",&m[i][0],&m[i][1]);
        int t=0;
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
            {
                fr[t].f=i;
                fr[t].e=j;
                fr[t++].w=dis(m[i][0],m[i][1],m[j][0],m[j][1]);
            }
        double ans=kruskal(n,t);
        printf("%.2f\n",ans);
    }
    return 0;
}

posted @ 2016-06-17 09:45  勿忘初心0924  阅读(211)  评论(0编辑  收藏  举报