zoukankan      html  css  js  c++  java
  • hdu1162 Eddy's picture (最小生成树之prim 算法)

    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
    #include<stdio.h>
    #include<math.h>
    typedef struct nn
    {
        double x,y,d;
    }Node;
    double map[105][105],sum,FN=10000000.0;
    int s[105],n;
    Node node[105];
    void count_edglen()
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            map[j][i]=map[i][j]=sqrt(pow(node[i].x-node[j].x,2)+pow(node[i].y-node[j].y,2));
            node[i].d=FN; s[i]=0;
        }
    }
    void prim()
    {
        double min;
        int m;
        s[1]=1; sum=0; m=1;
        for(int k=2;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            if(s[i]==0&&node[i].d>map[m][i])
            node[i].d=map[m][i];
    
            min=FN;
            for(int j=1;j<=n;j++)
            if(s[j]==0&&min>node[j].d)
            {
                m=j; min=node[j].d;
            }
            //printf("%.2lf %d  ",node[3].d,m);
            s[m]=1; sum+=min;
        }
    }
    int main()
    {
        while(scanf("%d",&n)>0)
        {
            for(int i=1;i<=n;i++)
            scanf("%lf%lf",&node[i].x,&node[i].y);
    
            count_edglen();
            prim();
            printf("%.2lf
    ",sum);
        }
    }
    


     
  • 相关阅读:
    android aar Could not find :ucrop-debug2.2.4:.
    OpenGL 实践之贝塞尔曲线绘制
    OpenGL 实现视频编辑中的转场效果
    简单易用的图像解码库介绍 —— stb_image
    博客图床迁移记
    Android 图片加载框架 Glide4.x
    Android 屏幕适配插件 ScreenMatch
    Android .9.png 的介绍
    Android 网络框架 Retrofit2
    Android 网络框架 OKHttp3
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3265440.html
Copyright © 2011-2022 走看看