zoukankan      html  css  js  c++  java
  • HDU 1162 Prim

    Eddy's picture
    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7287 Accepted Submission(s): 3689


    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



    Author

    eddy


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int inf=0x3fffffff;
    double cost[107][107];
    double ans;
    int n;
    struct node
    {
        double x,y;
    };
    node e[10007];
    bool used[107];
    double dis[107];
    void prim()
    {
        for(int i=1;i<=n;i++)
        {
            dis[i]=inf;
            used[i]=0;
        }
        dis[1]=0;
        while(1){
            int v=-1;
            for(int i=1;i<=n;i++)
            {
                if(!used[i]&&(v==-1||dis[i]<dis[v])) v=i;
            }
            if(v==-1) break;
            used[v]=1;
            for(int i=1;i<=n;i++)
            {
                if(!used[i]) dis[i]=min(dis[i],cost[v][i]);
            }
        }
       // for(int i=1;i<=n;i++)
          //  printf("%.2lf ",dis[i]);
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d",&n)){
               memset(e,0,sizeof(e));
               for(int i=0;i<=n;i++)
                 for(int j=0;j<=n;j++)
                   if(j==i) cost[i][j]=0;
                   else cost[i][j]=inf;
                for(int i=1;i<=n;i++) scanf("%lf%lf",&e[i].x,&e[i].y);
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=n;j++)
                {
                    cost[i][j]=cost[j][i]=sqrt((e[i].x-e[j].x)*(e[i].x-e[j].x)+(e[i].y-e[j].y)*(e[i].y-e[j].y));
                }
               prim();
                ans=inf;
                double sum=0;
                for(int i=1;i<=n;i++)
                {
                    sum+=dis[i];
                }
                printf("%.2lf
    ",sum);
        }
        return 0;
    }
    
    



  • 相关阅读:
    【vc】14_网络编程_socket编程
    【vc】1_Windows程序内部运行机制
    【vc】6_菜 单
    【vc】5_文本编程
    JZOJ 6481. 【GDOI2020模拟02.22】黎曼几何(矩阵乘法)
    JZOJ 6470. 【GDOI2020模拟02.13】小 B 的环(字符串哈希)
    A*(A-Star)搜索算法 入门详解
    JZOJ 4017. 【雅礼联考DAY01】逃跑(0/1分数规划+单调队列+线段树优化DP)
    JZOJ 6439. 【GDOI2020模拟01.17】小 ω 数排列(DP)
    JZOJ 100003. 【NOI2017模拟.4.1】 Tree(费用流)
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254379.html
Copyright © 2011-2022 走看看