zoukankan      html  css  js  c++  java
  • uva 10034

    Problem A: Freckles

    In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through.

    Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

    Input

    The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

    The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

    Output

    For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

    Sample Input

    1
    
    3
    1.0 1.0
    2.0 2.0
    2.0 4.0
    

    Sample Output

    3.41
    #include <iostream>
    #include <stack>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <fstream>
    #include <stack>
    #include <list>
    #include <sstream>
    #include <cmath>
    
    using namespace std;
    
    #define ms(arr, val) memset(arr, val, sizeof(arr))
    #define mc(dest, src) memcpy(dest, src, sizeof(src))
    #define N 105
    #define INF 0x3fffffff
    #define vint vector<int>
    #define setint set<int>
    #define mint map<int, int>
    #define lint list<int>
    #define sch stack<char>
    #define qch queue<char>
    #define sint stack<int>
    #define qint queue<int>
    /*最小生成树算法prim:稠密图(n*n),kruskal:稀疏图(e*lge)*/
    
    struct point
    {
        double x, y;
    }in[N];
    
    bool visit[N];
    double low[N];
    double g[N][N];
    int n, t;
    
    double multi2(int x1, int x2, int y1, int y2)
    {
        int x = x1 - x2;
        int y = y1 - y2;
        return x * x + y * y;
    }
    
    void prim()
    {
        ms(visit, 0);
        visit[0] = true;
        low[0] = INF;
        double ans = 0;
        for (int i = 1; i < n; i++)//初始化
        {
            low[i] = g[0][i];
        }
    
        int s = n - 1, _minp;
        double _min;
        while (s--)
        {
            _min = low[0];
            for (int i = 0; i < n; i++)//寻找最小边
            {
                if (!visit[i] && low[i] < _min)
                {
                    _min = low[i];
                    _minp = i;
                }
            }
            visit[_minp] = true;
            ans += sqrt(_min);
            for (int i = 0; i < n; i++)//更新low数组
            {
                if (!visit[i] && g[i][_minp] < low[i])
                {
                    low[i] = g[i][_minp];
                }
            }
        }
        printf("%.2lf
    ", ans);
        if (t)
        {
            putchar('
    ');
        }
    }
    
    int main()
    {
        scanf("%d", &t);
        while (t--)
        {
            scanf("%d", &n);
            for (int i = 0; i < n; i++)
            {
                scanf("%lf%lf", &in[i].x, &in[i].y);
                for (int j = i - 1; j >= 0; j--)
                {
                    g[i][j] = g[j][i] = multi2(in[i].x, in[j].x, in[i].y, in[j].y);
                }
            }
            prim();
        }
        return 0;
    }

  • 相关阅读:
    spark系列-6、对Application,Driver,Job,Task,Stage的理解
    spark系列-5、RDD、DataFrame、Dataset的区别和各自的优势
    spark系列-4、spark序列化方案、GC对spark性能的影响
    spark系列-2、Spark 核心数据结构:弹性分布式数据集 RDD
    nginx学习(九):跨域配置和防盗链配置
    nginx学习(八):nginx配置gzip
    nginx学习(七):nginx提供静态资源服务
    nginx学习(六):日志切割
    nginx学习(五):nginx.conf 核心配置文件详解
    nginx学习(四):nginx处理web请求机制
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3916875.html
Copyright © 2011-2022 走看看