2560. 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 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. 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

3

1.0 1.0

2.0 2.0

2.0 4.0

 

Sample Output

3.41

 

 

РЕШЕНИЕ

графы – минимальный остов

 

Анализ алгоритма

Помкольку количество веснушек не велико, задачу можно решить как алгоритмом Крускала, так и Прима.

Евклидово минимальное остовное дерево, как правило, следует находить при помощи алгоритма Прима.

 

Реализация алгоритма – Крускал

 

#include <cstdio>

#include <algorithm>

#include <vector>

#include <cmath>

#define MAX 110

using namespace std;

 

int mas[MAX];

double x[MAX], y[MAX],  MST;

vector<vector<double> > e;

vector<vector<double> >::iterator iter;

vector<double> temp(3,0);

 

int Repr(int n)

{

  while (n != mas[n]) n = mas[n];

  return n; 

}

 

int Union(int x,int y)

{

  int x1 = Repr(x),y1 = Repr(y);

  mas[x1] = y1;

  return (x1 != y1);

}

 

int lt(vector<double> a, vector<double> b)

{

  return (a[2] < b[2]);

}

 

double RunMST(void)

{

  double res = 0.0;

  for(iter = e.begin(); iter != e.end(); iter++)

    if (Union((*iter)[0],(*iter)[1]))

      res += (*iter)[2];

  return res;

}

 

int main(void)

{

  int i, j, n, m, tests;

  scanf("%d",&n);

  for(i = 1; i <= n; i++)

    scanf("%lf %lf",&x[i],&y[i]);

 

  for(i = 1; i <= n; i++) mas[i] = i;

   

  for(i = 1; i <= n; i++)

  for(j = i + 1; j <= n; j++)

  {

    temp[0] = i; temp[1] = j;

    temp[2] = sqrt((x[j] - x[i])*(x[j] - x[i]) +

                  (y[j] - y[i])*(y[j] - y[i]));

    e.push_back(temp);

  }

   

  sort(e.begin(),e.end(),lt);

  MST = RunMST();

 

  printf("%.2lf\n",MST);

  return 0;

}

 

Реализация алгоритма – Прим

 

#include <stdio.h>

#include <math.h>

#include <string.h>

#define MAX 110

 

double x[MAX], y[MAX];

int i, j, v, to, n;

int used[MAX], end_e[MAX];

double dist, min_e[MAX];

 

double dist2(int i, int j)

{

  return (x[j] - x[i])*(x[j] - x[i]) + (y[j] - y[i])*(y[j] - y[i]);

}

 

int main(void)

{

  scanf("%d",&n);

  for(i = 0; i < n; i++)

  {

    scanf("%lf %lf",&x[i], &y[i]);

    min_e[i] = 1e9;

  }

 

  memset(end_e,-1,sizeof(end_e));

  memset(used,0,sizeof(used));

 

  dist = min_e[0] = 0;

  for (i = 0; i < n; i++)

  {

    v = -1;

    for (j = 0; j < n; j++)

      if (!used[j] && (v == -1 || min_e[j] < min_e[v])) v = j;

 

    used[v] = 1;

    if (end_e[v] != -1) dist += sqrt(dist2(v,end_e[v]));

 

    for (to = 0; to < n; to++)

    {

      int dV_TO = dist2(v,to);

      if (!used[to] && (dV_TO < min_e[to]))

      {

        min_e[to] = dV_TO;

        end_e[to] = v;

      }

    }

  }

 

  printf("%.2lf\n",dist);

  return 0;

}