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;
    }
    



  • 相关阅读:
    深入揭秘HTTPS安全问题&连接建立全过程
    申请https证书需要注意的4大问题
    如何排查APP服务端和客户端是否支持ATS
    Apache和Nginx配置支持苹果ATS方法
    服务器配置ssl证书支持苹果ATS方法
    HTTPS背后的加密算法
    图解HTTPS协议加密解密全过程
    Java单例模式——并非看起来那么简单
    flask+mako+peewee(上)
    [转]ubuntu中查找软件的安装位置
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256407.html
Copyright © 2011-2022 走看看