zoukankan      html  css  js  c++  java
  • Eddy's picture


    title: Eddy's picture
    tags: [acm,杭电,最小生成树]

    题目链接

    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<bits/stdc++.h>
    using namespace std;
    double road[101][101];
    #define inf 0x3f3f3f3f
    struct node
    {
        double x;
        double y;
    };
    double distance1(node a, node b)
    {
        return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
    void prime(int n)
    {
        int vest[n];
        double dis[n];
    
        for (int i = 0; i < n; i++)
        {
            vest[i] = 0;
            dis[i] = road[0][i];
        }
        vest[0] = 1;
        int count1 = 1;
        int op = 0;
        double sum = 0;
        while (count1 < n)
        {
            double min1 = inf;
            for (int i = 0; i < n; i++)
            {
                if (vest[i] == 0 && dis[i] < min1)
                {
                    min1 = dis[i];
                    op = i;
                }
            }
            sum += min1;
            vest[op] = 1;
            count1++;
            for (int i = 0; i < n; i++)
            {
                if (vest[i] == 0 && dis[i] > road[op][i])
                {
                    dis[i] = road[op][i];
                }
            }
        }
        printf("%.2lf
    ", sum);
    }
    int main()
    {
        int n;
        while (~scanf("%d", &n))
        {
            node point[n];
            for (int i = 0; i < n; i++)
            {
                scanf("%lf%lf", &point[i].x, &point[i].y);
                
            }
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                        road[i][j] = 0;
                    else road[i][j] = distance1(point[i], point[j]);
                }
            prime(n);
    
    
        }
        return 0;
    }
    /*
    3
    1.0 1.0
    2.0 2.0
    2.0 4.0
    */
    
    
  • 相关阅读:
    URL传参到servlet含特殊字符——#号无法传递
    textarea标签中间出现空格问题
    MySQL学习笔记(23)——自定义函数
    修改日期插件问题
    获取Spring的ApplicationContext的方法
    数据库用户被锁
    ava获得当前文件路径
    前端框架
    LeetCode 485. 最大连续1的个数
    LeetCode 283. 移动零
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/6708238.html
Copyright © 2011-2022 走看看