zoukankan      html  css  js  c++  java
  • zoj 1718 poj 2031 Building a Space Station

    最小生成树,用了Kruskal算法。POJ上C++能过,G++不能过。。。 算出每两个圆心之间的距离,如果距离小于两半径之和,那么这两个圆心之间的距离直接等于0,否则等于距离-R[i]-R[j]。

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    const int maxn = 10100;
    struct abc{ int start, end; double path; }node[maxn];
    bool cmp(const abc&a, const abc&b){ return a.path < b.path; }
    double x[maxn], y[maxn], z[maxn], r[maxn];
    int father[maxn];
    int find(int x)
    {
        if (x != father[x]) father[x] = find(father[x]);
        return father[x];
    }
    int main()
    {
        int n, i, j;
        while (~scanf("%d", &n))
        {
            if (n == 0) break;
            int tot = 0;
            for (i = 1; i <= n; i++) scanf("%lf%lf%lf%lf", &x[i], &y[i], &z[i], &r[i]);
            for (i = 0; i <= n; i++) father[i] = i;
            for (i = 1; i <= n; i++)
            {
                for (j = i + 1; j <= n; j++)
                {
                    node[tot].start = i;
                    node[tot].end = j;
                    double tt = sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]) + (z[i] - z[j])*(z[i] - z[j]));
                    if (tt <= r[i] + r[j]) node[tot].path = 0;
                    else node[tot].path = tt - r[i] - r[j];
                    tot++;
                }
            }
            double ans = 0;
            sort(node, node + tot, cmp);
            for (i = 0; i < tot; i++)
            {
                int xx = find(node[i].start);
                int yy = find(node[i].end);
                if (xx != yy)
                {
                    father[xx] = yy;
                    ans = ans + node[i].path;
                }
            }
            printf("%.3lf
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    Load与onload的区别
    理解.NET中的CLR原理(二)
    用JS实现网站的繁体简体版
    JQUERY获取text,areatext,radio,checkbox,select值
    SQL Server 存储过程的分页方案比拼
    理解.NET中的CLR原理(三)
    请慎用ASP.Net的validateRequest="false"~
    请慎用ASP.Net的validateRequest="false"~
    GridView中的超链接
    GridView中的超链接
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4482354.html
Copyright © 2011-2022 走看看