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);
        }
    }
    


     
  • 相关阅读:

    c#常用的基础概念
    Visual Studio2010+SOS.dll调试入门 摘自 http://www.cnblogs.com/luminji/archive/2011/01/27/1946217.html
    优化数据库之前的10个问题
    js数组清空的两种方式
    我的资源(网站, 工具)
    iis使用十大原则
    SQL2005分页存储过程
    温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
    无法获得数据库 'model' 上的排他锁 网上搜索结果 IT
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3265440.html
Copyright © 2011-2022 走看看