POJ2560 (minimum spanning tree template problem)

Source: Internet
Author: User

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 Li Berty Bell. Alas, one of the freckles turns out to is a scar, so his Ripley ' s engagement falls through.
Consider Dick's back is a plane with freckles at various (x, y) locations. Your job is-to-tell Richie-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 was done there must was a sequence of connected lines from any freckle to any other freckle.

Input

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

Output

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

Sample Input

31.0 1.02.0 2.02.0 4.0

Sample Output

3.41

Test instructions: give you n points, the least amount of ink used to connect the n dots.

Minimum spanning Tree template title: Once the two points have been wired, then there will be no longer connected, otherwise it will waste ink, then use the idea of the collection, once the two points of the Father node is the same, then they have a line between them, no longer connected. At least n points need to be n-1 to the edge of the connection. Note POJ's topic can be used in C + + do not use g++, because inexplicable will be wrong

Please look at the code:

#include <iostream>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace Std;

struct node
{
Double x, y;
};

struct code
{
int x, y;
Double V;
};

BOOL CMP (code A,code B)
{
Return a.v<b.v;
}

int const N=110;
Node A[n];
int father[n];
Code B[n*n];

int Find (int a)
{
int b=a;
while (B!=father[b]) b=father[b];
while (A!=father[a])
{
int X=father[a];
Father[a]=b;
A=x;
}
return b;
}

void Union (int x,int y)
{
int Fx=find (x);
int Fy=find (y);
if (fx!=fy) father[fx]=fy;
}

int main ()
{
int m;
while (~SCANF ("%d", &m))
{
for (int i=1;i<=m;i++) scanf ("%lf%lf", &a[i].x,&a[i].y);
for (int i=1;i<=m;i++) father[i]=i;
int p=1;
for (int i=1;i<m;i++)
{
for (int j=i+1;j<=m;j++)
{
B[p].x=i;
B[p].y=j;
B[p].v=sqrt ((a[i].x-a[j].x) * (a[i].x-a[j].x) + (A[I].Y-A[J].Y) * (A[I].Y-A[J].Y));
p++;
}
}
Sort (b+1,b+p,cmp);
Double ans=0;
int k=0;
for (int i=1;i<p;i++)
{
if (Find (b[i].x) ==find (b[i].y) | | K==M-1) continue;
Union (B[I].X,B[I].Y);
ANS+=B[I].V;
k++;
}
printf ("%.2lf\n", ans);
}
return 0;
}

POJ2560 (minimum spanning tree template problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.