zoukankan      html  css  js  c++  java
  • POJ 2560 浮点型的带权值

    Freckles
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8309   Accepted: 3952

    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

    3
    1.0 1.0
    2.0 2.0
    2.0 4.0
    

    Sample Output

    3.41
    


    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    struct edge
    {
        int x,y;
        float w;
    }e[1000000];
    int f[1000000];
    float x[1000];
    float y[1000];
    int cmp(edge a,edge b)
    {
        return a.w<b.w;
    }
    int find(int x)
    {
        return f[x] == x ? x : (f[x] = find(f[x])); //并查集一步写完
    }
    int merge(int a,int b)
    {
        int u,v;
        u=find(a);
        v=find(b);
        if(u!=v){
        f[v]=u;
        return 1;
    }
    return 0;
    }
    int main()
    {
        int n;
        int cnt=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++)
            {
                e[cnt].x=i;
                e[cnt].y=j;
                e[cnt].w=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
                cnt++;
            }
        }
        float ans=0;
        sort(e,e+cnt,cmp);
        for(int i=0;i<cnt;i++)
        {
    
    
               if(merge(e[i].x,e[i].y))
                 ans+=e[i].w;
            }
    
        printf("%.2f",ans);
    
    return 0;
    }
    



  • 相关阅读:
    MT【296】必要性探路
    MT【295】线段比的仿射变换
    MT【294】函数定义的理解
    MT【293】拐点处切线
    MT【292】任意存在求最值
    MT【291】2元非齐次不等式
    MT【290】内外圆求三角最值
    MT【289】含参绝对值的最大值之三
    MT【288】必要性探路
    Xadmin-自定义字段支持实时编辑
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256407.html
Copyright © 2011-2022 走看看