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
    */
    
    
  • 相关阅读:
    SQL基础复习03--数据查询SQL语句(单表查询)
    SQL基础复习02--数据操纵SQL语句
    数据结构与算法01--复杂度
    SQL基础复习01--SQL基础与数据定义SQL语句
    Azure Data Studio的初步了解与使用
    ASP.NET Core Web API 使用DynamicLinq实现排序功能
    Vue3-说说Vue 3.0中Treeshaking特性?举例说明一下?
    JavaScript高频手写面试题
    Java常用文件操作-1
    Java 架构师之路(2)
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/6708238.html
Copyright © 2011-2022 走看看