zoukankan      html  css  js  c++  java
  • 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 ",ans);
        }
        return 0;
    }

  • 相关阅读:
    tabbar 旋转指定的页面
    GDAL中文路径不能打开&Shp文件字段属性值中文乱码
    Project : error PRJ0019: 工具从"Moc'ing xxx.h..."
    详解Android中的屏幕方向
    qt中获取文件路径和文件名
    vs2005下Qt项目中修改exe图标的方法
    Qt & C/C++统计运行时间
    Qt 中Treewidget添加右键菜单
    QT 中文乱码解决方案
    Qt多线程应用QRunnable显示进度条示例
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5781528.html
Copyright © 2011-2022 走看看