Eddy's picture (kruskal)

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

 问题描述:给你一张图纸上的一些坐标点,每一个点都用墨水和直线相连,使所有的点最终链接在同一个地方。你的职责是找出墨水画的最短长度是多少?

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const int N=100+5;

    int n,m,t;
    int fa[N*(N-1)];
    int i,j,k;
struct point
{
    double x,y,id;
}p[N];
struct Edge
{
    double u,v,w;
    bool const operator<(const Edge &b)
    const{
        return w<b.w;
    }
}edge[N*(N-1)];
int Find(int son)
{
    return son==fa[son] ? son : fa[son]=Find(fa[son]);
}
bool Union(int x,int y)
{
    x=Find(x),y=Find(y);
    if(x==y) return 1;
    else{
        fa[y]=x;
        return 0;
    }
}
int main()
{
    cin.tie(0);istream::sync_with_stdio(false);
    for(;cin>>n;){
        for(i=1;i<=n;i++) fa[i]=i;
        for(i=1;i<=n;i++){
            cin>>p[i].x>>p[i].y;
        }

        int num=0;
        for(i=1;i<=n;i++){
            for(j=1+i;j<=n;j++){
                edge[++num].u=i;
                edge[num].v=j;
                edge[num].w=sqrt(pow(p[i].x-p[j].x,2.0)+pow(p[i].y-p[j].y,2.0));
            }
        }

        double ans=0;
        sort(edge+1,edge+1+num);
        for(i=1;i<=num;i++){
            if(0==Union(edge[i].u,edge[i].v))
                ans+=edge[i].w;
        }
        printf("%.2lf\n",ans);
    }
}
原创文章 413 获赞 16 访问量 3万+

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/106120109
今日推荐