POJ 2560

来源:互联网 发布:淘宝质量好的牛仔裤 编辑:程序博客网 时间:2024/06/15 11:02

Freckles
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8055 Accepted: 3862

Description

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

31.0 1.02.0 2.02.0 4.0

Sample Output

3.41

Source

Waterloo local 2000.09.23

题目大意:给你n个点,让你把他们全部链接起来,使得用的笔油最少(就这么理解就可以了~不要吐槽~~)

很典型的最小生成树的题目,我们这里把所有的点距离求出来,然后对点也标记上序号、这里注意精度控制问题,double会损失精度,我们要用float变量类型才行,这里直接上代码:

#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;struct path{    int x,y;    float val;}a[1000000];int f[1000000];float x[1000];float y[1000];int cmp(path a,path b){    return a.val<b.val;}int find(int x){    return f[x] == x ? x : (f[x] = find(f[x]));}void merge(int a,int b){    int A,B;    A=find(a);    B=find(b);    if(A!=B)    f[B]=A;}int main(){    int n;    int cont=0;    scanf("%d",&n);    for(int i=0;i<1000000;i++)    {        f[i]=i;    }    for(int i=0;i<n;i++)    {        scanf("%f%f",&x[i],&y[i]);        for(int j=0;j<i;j++)        {            a[cont].x=i;            a[cont].y=j;            a[cont].val=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));            cont++;        }    }    float output=0;    sort(a,a+cont,cmp);    for(int i=0;i<cont;i++)    {        if(find(a[i].x)!=find(a[i].y))        {            merge(a[i].x,a[i].y);            output+=a[i].val;        }    }    printf("%.2f",output);}








0 0
原创粉丝点击