zoukankan      html  css  js  c++  java
  • HDOJ 1162 Eddy's Picture (最小生成树)

    Eddy's picture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8206    Accepted Submission(s): 4164


    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
     
    给出n,然后n个点的坐标,求出将这些点用线全部连接起来最短的路径。先保存下所有点的坐标,然后对于任意不相同的亮点计算下距离加入到图中,因为边的数目比较多,就用prim算法了。
    #include<iostream>
    #include<vector>
    #include<cstdio>
    #define MAXN 105
    #include<cmath>
    using namespace std;
    int m,n;
    const int inf=1<<31-1;
    double point[MAXN][2];
    vector<int>mmap[MAXN];
    double val[MAXN][MAXN];
    int vis[MAXN];
    double  prim()
    {
        double  re=0;
        int k=0;
        double d[MAXN];
        fill(d,d+n+1,inf);
        fill(vis,vis+n+1,0);
        d[1]=0;
        while(true)
        {
            int v=-1;
            for(int i=1;i<=n;i++){
                if(!vis[i]&&(v==-1||d[i]<d[v]))
                    v=i;
            }
           /* if(v==-1||d[v]==inf)
                    return -1;*/
            re+=d[v];
            k++;
            if(k==n)return re;
            vis[v]=1;
            for(int i=0;i<mmap[v].size();i++)
            {
                int x=mmap[v][i];
                if(!vis[x]&&val[v][x]<d[x])
                    d[x]=val[v][x];
            }
    
        }
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            m=0;
            for(int i=1;i<=n;i++)
             {
                 mmap[i].clear();
                 scanf("%lf%lf",&point[i][0],&point[i][1]);
             }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<i;j++)
                {
                    m++;
                    double x1=point[i][0],y1=point[i][1];
                    double x2=point[j][0],y2=point[j][1];
                    mmap[i].push_back(j);
                    mmap[j].push_back(i);
                    val[i][j]=val[j][i]=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    
                }
            }
           printf("%.2lf
    ",prim());
        }
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    洛谷 P1886 滑动窗口 (单调队列)
    Acwing 288.休息时间 (环形DP)
    Acwing 287.积蓄程度 (树形DP换根)
    2020 Multi-University Training Contest 5 Tree (树形DP)
    剑指offer-JZ50-数组中的重复数字(C++)
    假设以下有一个结构体存放的是学生的记录,每条记录包括:学号、姓名、成绩
    剑指offer-JZ48-不用加减乘除做加法(C++)
    剑指offer-JZ51-构建乘积数组
    数据结构与算法->递归
    力扣(LeetCode)试题6-Z字形变换 C++代码
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768440.html
Copyright © 2011-2022 走看看