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;
    }

  • 相关阅读:
    管理经济学之第三章(消费者效用分析)
    管理经济学之第二章(供求分析)
    JVM之GC回收信息详解
    管理经济学之第一章(导论)
    JAVA的引用类型
    6.jQuery动画和队列,简单的queue()入队和dequeue()出队实现
    5.jQuery实现简单的on()和trigger()方法
    4.jQuery的clone()方法和data()方法
    3.jQuery操作DOM对象的方法
    2.jQuery简单实现get()和eq()和add()和end()方法
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3916875.html
Copyright © 2011-2022 走看看