zoukankan      html  css  js  c++  java
  • Lonlife 1000

    1000 - Spoon Devil's 3-D Matrix

    Time Limit:1s Memory Limit:32MByte

    Submissions:208Solved:65

    DESCRIPTION

    Spoon Devil build a 3-D matrix, and he(or she) wants to know if he builds some bases what's the shortest distance to connect all of them.

    INPUT
    There are multiple test cases. The first line of input contains an integer T
    , indicating the number of test cases. For each test case:The first line contains one integer n (0<n<50), indicating the number of all points. Then the next n lines, each lines contains three numbers xi,yi,zi indicating the position of i
    -th point.
    OUTPUT
    For each test case, output a line, which should accurately rounded to two decimals.
    SAMPLE INPUT
    221 1 02 2 031 2 30 0 01 1 1
    SAMPLE OUTPUT
    1.413.97


    思路:

    三维点的MST(Krusal)

    #include<iostream>
    #include<cmath>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    const int MAXN=55;
    const int MAXM=1200;
    int pre[MAXN];
    struct node
    {
        double x,y,z;
        node()
        {
            x=y=z=0;
        }
    }Node[MAXN];
    struct edge
    {
        int s,e;
        double d;
        edge()
        {
            s=e=d=0;
        }
    }Edge[MAXM];
    bool cmp(edge a, edge b)
    {
        return a.d<b.d;
    }
    int father(int x)
    {
        if(pre[x]==x)
            return x;
        else
        {
            pre[x]=father(pre[x]);
            return pre[x];
        }
    }
    double krusal(int n)
    {
        double cost=0;
        for(int i=0;i<MAXN;i++)pre[i]=i;
        int cnt=0;
        int index=0;
        while(cnt<n-1)
        {
            int ps=father(Edge[index].s);
            int pe=father(Edge[index].e);
            if(ps!=pe)
            {
                pre[ps]=pe;
                cost+=Edge[index].d;
                cnt++;
            }
            index++;
        }
        return cost;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%lf%lf%lf",&Node[i].x,&Node[i].y,&Node[i].z);
            }
            int cnt=0;
            for(int i=0;i<n;i++)
            {
                for(int j=i+1;j<n;j++)
                {
                    Edge[cnt].s=i;Edge[cnt].e=j;
                    Edge[cnt].d=sqrt(pow(fabs(Node[i].x-Node[j].x),2.0)+pow(fabs(Node[i].y-Node[j].y),2.0)+pow(fabs(Node[i].z-Node[j].z),2.0));
                    cnt++;
                }
            }
            sort(Edge,Edge+cnt,cmp);
            printf("%.2lf
    ",krusal(n));
        }
        return 0;
    }
    


  • 相关阅读:
    在UltraEdit中如何像NotePad++一样实现双击单词在全文中高亮
    记人生第一次做面试官的经历
    error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug
    压缩感知中的数学知识:稀疏、范数、符号arg min
    Tensorflow timeline trace
    tensorflow serving
    日志分析工具ELK(一)
    Zabbix3.0安装部署最佳实践
    防cc攻击利器之Httpgrard
    反向代理负载均衡之haproxy
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7776022.html
Copyright © 2011-2022 走看看