zoukankan      html  css  js  c++  java
  • HDU 1162 Eddy's picture (最小生成树 普里姆 )

    题目链接

    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个点链接起来,这就是简单的最小生成树的问题,将任意两个点之间的距离计算出来,看作他们之间的路径长度,这样的话就能很好的理解为什么是最小生成树的问题了。

    代码:

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #define INF 1 << 30
    double a[101] , b[101] , map[101][101] ;
    double dis[101] ;
    int used[101] ;
    void prim(int n)
    {
        int c = 0 ;
        int i = 0 , j = 0 ;
        double sum = 0 ;
        dis[1] = 0 ;
        for(i = 1 ; i <= n ; i++)
        {
            double min = INF ;
            c = 0 ;
            for(j = 1 ; j <= n ; j++)
            {
                if(!used[j] && dis[j] < min)
                {
                    min = dis[j] ;
                    c =j ;
                }
            }
            used[c] = 1 ;
            for(j  = 1 ; j <= n ; j++ )
            {
                if(!used[j] && dis[j] > map[c][j])
                    dis[j] = map[c][j] ;
            }
        }
    
        for(i = 1 ; i <= n ; i++)
            sum += dis[i] ;
        printf("%.2lf
    ",sum);
    }
    int main()
    {
        int n = 0 ;
        while(~scanf("%d",&n))
        {
            memset(a , 0 , sizeof( a ) ) ;
            memset(b , 0 , sizeof( b ) ) ;
            int i = 0 , j = 0 ;
            for(i = 1 ; i <= n ; i++)
            {
                for(j = 1 ; j <= n ; j++)
                    map[i][j] = INF ;
                dis[i] = INF ;
                used[i] = 0 ;
            }
            for(i = 1 ; i <= n ; i++)
            {
                scanf("%lf%lf" , &a[i] , &b[i] );///切记不能向一般的图论的问题,在输入的同时保存路径
            }
            double m = 0 , x = 0;
            for(i = 1 ; i <= n ; i++ )///应该在输入结束之后,将任意两点之间的路径计算出来
            {
                for(j = 1 ; j <= n ; j++)
                {
                    x = (a[j]-a[i])*(a[j]-a[i])+(b[j]-b[i])*(b[j]-b[i]) ;
                    m = sqrt( x ) ;
                    map[i][j] = map[j][i] = m ;
                }
            }
            prim( n );
        }
        return 0 ;
    }
  • 相关阅读:
    js拖拽效果 javascript实现将元素拖拽如某容器效果demo
    使用 transform3D 造成网页闪动的底层原因剖析
    设置文字垂直 竖向 显示
    文本光标,高亮选中一些出来
    HTMl5的sessionStorage和localStorage
    event 事件兼容性处理 keycode 大全
    收藏个支持进度条与文件拖拽上传的js File Uploader
    three.js 3D效果
    Winform下的地图开发控件(GMap.NET)使用心得
    ASP.NET Forms验证 实现子域名(SubDomain)共享登陆下的缺陷
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6795916.html
Copyright © 2011-2022 走看看