首页 > 代码库 > uva 10034 - Freckles

uva 10034 - Freckles

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

131.0 1.02.0 2.02.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\n", ans);    if (t)    {        putchar(\n);    }}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;}