poj2031 Building a space station the smallest spanning tree in three dimensions

Source: Internet
Author: User

Topic Links:

2031

Http://write.blog.csdn.net/postedit?ref=toolbar




Test instructions

In three-dimensional space, there are n spherical space stations, which give the three-dimensional coordinates x, Y, z and radius of each space station. There may be intersections, inclusions, and other conditions between space stations. If not, the distance between the two space stations is 0. Minimum spanning tree for all space stations.




Exercises

Three-dimensional composition the distance between the two space stations equals two circle distance minus the radius of two circles if the distance is less than 0, the distance is 0

In the set Kruskal template can be




Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <    cmath>using namespace std;struct node{int u,v; Double W;} EDGE[10005];d ouble map[105][4],ans;int fa[105];int m,s;int cmp (node A,node b) {return A.W&LT;B.W;}    int find (int x) {int d=x,t;    while (fa[d]>=0) d=fa[d];        while (X!=d)//compression path {t=fa[x];        Fa[x]=d;    x=t; } return D;}    void Kruskal () {int r1,i,j,r2,n=0,ss;        for (i=0; i<s; i++) {r1=find (edge[i].u), R2=find (EDGE[I].V);            if (R1!=R2) {n++;            SS=FA[R1]+FA[R2];            ANS+=EDGE[I].W;            if (FA[R1]&LT;FA[R2]) fa[r1]=ss,fa[r2]=r1;        else Fa[r1]=r2,fa[r2]=ss;    } if (n==m-1) break; } return;    int main () {int i,j;    Double weight;        while (scanf ("%d", &m) &&m) {ans=s=0;        memset (fa,-1,sizeof (FA)); for (I=1; i<=m; i++) {scanf ("%lf%lf%lf%lf", &map[i][0],&map[i][1],&map[i][2],&map[i][3]);                for (j=1; j<i; J + +) {edge[s].u=j,edge[s].v=i;                Weight=sqrt (Pow (map[i][0]-map[j][0],2) +pow (map[i][1]-map[j][1],2) +pow (map[i][2]-map[j][2],2));                if (weight>map[i][3]+map[j][3])//distance edge[s++].w=weight-map[i][3]-map[j][3];            else edge[s++].w=0;        }} sort (edge,edge+s,cmp);        Kruskal ();    printf ("%.3lf\n", ans); } return 0;}



poj2031 Building a space station the smallest spanning tree in three dimensions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.