AOJ1127 Building a Space Station

Building a Space Station

You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. The space station is made up with a number of units, called cells.

最小全域木を作る.小数点桁ぐらい出力していて, 桁固定というのを見逃していた.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <cmath>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
#define EPS 1e-8
#define equals(a,b) fabs((a) - (b)) < EPS

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

struct Point3D {
  double x, y, z;

  Point3D() : x(0), y(0), z(0) {}

  Point3D(double x, double y, double z) : x(x), y(y), z(z) {}

  Point3D operator+(const Point3D &o) const { return Point3D(x+o.x, y+o.y, z+o.z); }

  Point3D operator-(const Point3D &o) const { return Point3D(x-o.x, y-o.y, z-o.z); }

  Point3D operator*(const double m) const { return Point3D(x*m, y*m, z*m); }

  Point3D operator/(const double d) const { return Point3D(x/d, y/d, z/d); }

  bool operator==(const Point3D &o) const { return fabs(x-o.x) < EPS && fabs(y-o.y) < EPS; }
};

ostream& operator << (ostream& os, const Point3D& p) {
  os << "(" << p.x << ", " << p.y << ", " << p.z << ")";
  return os;
}

double dot(Point3D a, Point3D b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Point3D cross(Point3D a, Point3D b) { return Point3D(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); }

double norm(Point3D p) { return dot(p, p); }
double abs(Point3D p) { return sqrt(norm(p)); }

struct Line {
  Point3D a, b;

  Line() : a(Point3D(0, 0, 0)), b(Point3D(0, 0, 0)) {}

  Line(Point3D a, Point3D b) : a(a), b(b) {}
};

ostream& operator << (ostream& os, const Line& l) {
  os << "(" << l.a.x << ", " << l.a.y << ", " << l.a.z <<  ")-(" << l.b.x << "," << l.b.y << ", " << l.b.z <<  ")";
  return os;
}

Point3D project(Line l, Point3D p) {
  Point3D base = l.b - l.a;
  double t = dot(base, p-l.a) / dot(base, base);
  return l.a + base * t;
}

struct Sphere {
  Point3D p;
  double r;

  Sphere() : p(Point3D(0, 0, 0)), r(0.0) {}

  Sphere(Point3D p, double r) : p(p), r(r) {}
};

ostream& operator << (ostream& os, const Sphere& s) {
  os << "(" << s.p.z << ", " << s.p.y << ", " << s.p.z << " :" << s.r << ")";
  return os;
}

double distanceSphSph(Sphere s1, Sphere s2) {
  Point3D p = s2.p - s1.p;
  double dist = abs(p) - s1.r - s2.r;
  return max(0.0, dist);
}

struct UnionFind {
  vector<int> par,rank;
  int N;

  UnionFind(int n) {
      N = n;
      par.resize(n);
      rank.resize(n);

      rep(i,n) {
          par[i] = i;
          rank[i] = 0;
      }
  }

  int find(int x) {
      if(par[x] == x) return x;
      else return par[x] = find(par[x]);
  }

  void unite(int x,int y) {
      x = find(x);
      y = find(y);

      if(x == y) return;

      if(rank[x] < rank[y]) {
          par[x] = y;
      }
      else {
          par[y] = x;
          if(rank[x] == rank[y]) rank[x]++;
      }
  }

  bool same(int x,int y) {
      return find(x) == find(y);
  }

  int size() {
      int cnt = 0;
      rep(i,N) if(find(i) == i) cnt++;
      return cnt;
  }
};

struct edge {
  int from,to;
  double cost;

  edge(int t, int c) : to(t),cost(c) {}
  edge(int f, int t, double c) : from(f),to(t),cost(c) {}

  bool operator<(const edge &e) const {
      return cost < e.cost;
  }
};

double kruskal(int n, vector<edge> v) {
  sort(v.begin(),v.end());
  UnionFind uf(n);

  double ret = 0;
  rep(i, v.size()) {
      edge e = v[i];
      if(!uf.same(e.from,e.to)) {
          uf.unite(e.from,e.to);
          ret += e.cost;
      }
  }
  return ret;
}

int main() {
  int n;

  while(cin >> n && n) {
      vector<Sphere> s(n);
      rep(i, n) cin >> s[i].p.x >> s[i].p.y >> s[i].p.z >> s[i].r;

      vector<edge> v;
      rep(i, n) {
          REP(j, i+1, n) {
              double dist = distanceSphSph(s[i], s[j]);
              v.push_back(edge(i, j, dist));
          }
      }

      cout << fixed;
      cout.precision(3);
      cout << kruskal(n, v) << endl;
  }

  return 0;
}
Mar 23rd, 2016