A MST Problem

Problem H: A MST Problem

Time Limit: 1 Sec   Memory Limit: 32 MB
Submit: 5   Solved: 4
[ Submit][ Status][ Web Board]

Description

It is just a mining spanning tree ( 最小生成树 ) problem, what makes you a little difficult is that you are in a 3D space.

Input

The first line of the input contains the number of test cases in the file. And t he first line of each case
contains one integer numbers n(0<n<30) specifying the number of the point . The n next n line s, each line
contain s Three Integer Numbers xi,yi and zi, indicating the position of point i.

Output

For each test case, output a line with the answer, which should accurately rounded to two decimals .

Sample Input

2
2
1 1 0
2 2 0
3
1 2 3
0 0 0
1 1 1

Sample Output

1.41
3.97

HINT


#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
const int infinity=99999999;
const int maxnum=105;
double map1[maxnum][maxnum];
bool visited[maxnum];
double low[maxnum];
int nodenum;
struct node
{
    int x;
    int y;
    int z;
}nd[105];
double prim()
{
    int i,j,pos=1;
    double result,Min;
    memset(visited,0,sizeof(visited));//初始化都未标记
    result=0;
    for(i=1;i<=nodenum;i++)
    {
        low[i]=map1[pos][i];
    }
    visited[pos]=1;//把1号作为起点
    for(i=2;i<=nodenum;i++)//这个i没有其他的意思就是一个循环次数
    {
        Min=infinity;pos=-1;//从1号开始找最小的边
        for(j=1;j<=nodenum;j++)
        {
            if(!visited[j]&&Min>low[j])
            {
                Min=low[j];
                pos=j;
            }
        }
        if(pos==-1)
        {
            return -1;
        }
        visited[pos]=1;//做到与1os号最近的边
        result+=Min;//加权值
        for(j=1;j<=nodenum;j++)
        {
            if(!visited[j]&&low[j]>map1[pos][j])
            {
                low[j]=map1[pos][j];//这个就是替换未被标记的最小权值!
            }
        }
    }
    return result;
}
int main()
{
    int n,s,e,w,i,j,t,x,y,z;
    double lenth,ans;
    cin>>t;
    while(t--)
    {
        cin>>n;
        nodenum=n;
        for(i=1;i<=nodenum;i++)
        {
            for(j=1;j<=nodenum;j++)
            {
                map1[i][j]=infinity;
            }
        }
        for(i=1;i<=n;i++)
        {
            cin>>nd[i].x>>nd[i].y>>nd[i].z;
        }
        for(i=1;i<=n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                lenth=sqrt((nd[i].x-nd[j].x)*(nd[i].x-nd[j].x)+(nd[i].y-nd[j].y)*(nd[i].y-nd[j].y)+(nd[i].z-nd[j].z)*(nd[i].z-nd[j].z));
              //  printf("djklsajiofgioj%.2lf\n",lenth);
                map1[i][j]=map1[j][i]=lenth;
            }
        }
        ans=prim();//开始rim算法
        if(ans==-1)
        {
            cout<<"?"<<endl;
        }
        else
        {
            printf("%.2lf\n",ans);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值