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 1936 All in All
POJ 1936 All in All
64 0
|
算法 数据建模 机器学习/深度学习
POJ 1012 Joseph
Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53862   Accepted: 20551 Description The Joseph's problem is notoriously known.
818 0
poj 1455
Description n participants of > sit around the table. Each minute one pair of neighbors can change their places.
599 0
|
测试技术
poj-1218 THE DRUNK JAILER 喝醉的狱卒
自己去看看原题; 题目大意: 就是一个狱卒喝醉了,他第一趟吧所有的监狱都带开,第二趟把能把二整除的监狱关闭,第三趟操作能把三整除的监狱; 求最后能逃跑的罪犯数 输入第一个数是代表 测试数据组数 每个数据代表狱卒来回的次数 当作开关问题即可 #include using names...
961 0
POJ 2487 Stamps
Description Background Everybody hates Raymond. He’s the largest stamp collector on planet earth and because of that he always makes fun of all the others at the stamp collector parties.
1036 0