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



  • 相关阅读:
    Linux文件系统命令 cd
    Linux文件系统命令 cat
    正则表达式(.+?)与(.+)区别
    Linux文件系统命令 ls
    如何正确认识Docker Kubernetes 和 Apache Mesos
    基于Nutch Solr等基于搭建一体化的数据抓取平台
    ubuntu apache ssl配置
    机器学习入门
    docker 安全性问题
    数据工程师面试必备——Python与数据库的那些事
  • 原文地址:https://www.cnblogs.com/mingrigongchang/p/6246256.html
Copyright © 2011-2022 走看看