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


     
  • 相关阅读:
    Angular--ui-router的使用
    一个简单的俄罗斯方块游戏脚本
    c中使用gets() 提示warning: this program uses gets(), which is unsafe.
    最短路径之Floyd算法
    动态规划求最大子序列
    Mac上编译C++报错
    nginx location匹配规则
    nginx配置入门
    拿什么守护你的Node.JS进程: Node出错崩溃了怎么办? foreverjs, 文摘随笔
    ASP.NET 上的 Async/Await 简介
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3265440.html
Copyright © 2011-2022 走看看