poj2031 kruskal

简介: http://poj.org/problem?id=2031 #include<iostream> #include<algorithm> #include<cmath> #include<cstdio> using namespace std; double a[101][4]; double esp = 0.0000001;

http://poj.org/problem?id=2031

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
double a[101][4];
double esp = 0.0000001;
struct edge
{
    int u,v;
    double w;
} e[5000];
int tree[101];
int cmp( const void *a ,const void *b)
{
    return (*(edge *)a).w > (*(edge *)b).w ? 1 : -1 ;
}
int main()
{
    int n;
    //freopen("1.txt","r",stdin);
    while (cin>>n,n)
    {
        int i,j;
        for (i = 0; i < n; ++i)
            cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3];
        int k = 0;
        for (i = 0; i < n-1; ++i)
            for (j = i+1; j < n; ++j)
            {
                e[k].u = i;
                e[k].v = j;
                double temp = sqrt( (a[i][0]-a[j][0])*(a[i][0]-a[j][0])
                                    +(a[i][1]-a[j][1])*(a[i][1]-a[j][1])
                                    +(a[i][2]-a[j][2])*(a[i][2]-a[j][2]) )- a[i][3] - a[j][3];
                if (temp < esp)
                    e[k].w = 0;
                else
                    e[k].w = temp;
                ++k;
            }
        qsort(e,k,sizeof(e[0]),cmp);
        for (i = 0; i < n; ++i)
            tree[i] = i;
        double total = 0;
        for (i = 0; i < k; ++i)
        {
            int m1 = tree[e[i].u];
            int m2 = tree[e[i].v];
            if (m1 != m2)
            {
                total += e[i].w;
                for (j = 0; j < n; ++j)
                    if (tree[j] == m2)
                        tree[j] = m1;
            }
        }
        printf("%.3f\n",total);
    }
    return 0;
}

  

目录
相关文章
POJ 3767
I Wanna Go Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2488   Accepted: 1041 Description The country i...
750 0
|
人机交互
poj 2524 Ubiquitous Religions
点击打开链接poj 2524 思路:简单的并查集 分析:输入的时候把相同集合的元素合并在一起,那么我们用一个vis数组来标记集合根节点是否出现过。
857 0
|
9月前
poj-1611-The Suspects
poj-1611-The Suspects
40 0
poj 2528 Mayor's posters
点击打开链接poj2528 思路:离散化+线段树成段更新 分析: 1 首先这一题的数据是错误的,这题的区间的最大值为10000000,如果我们按照正常的线段树的思路去做的话肯定是会超内存和超时的。
1119 0
|
人工智能 vr&ar
POJ 1067 取石子游戏
取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40917   Accepted: 13826 Description 有两堆石子,数量任意,可以不同。
1132 0

热门文章

最新文章