class unionfind: def __init__(self,uni_num): self.uni_num=uni_num self.union_root = [-1 for i in range(self.uni_num + 1)] self.union_depth = [0] * (self.uni_num + 1) self.e_num=[0]*(self.uni_num+1) def find(self,x): # 親は誰? if self.union_root[x] < 0: return x else: self.union_root[x] = self.find(self.union_root[x]) return self.union_root[x] def unite(self,x, y): x = self.find(x) y = self.find(y) if x == y: self.e_num[x]+=1 return if self.union_depth[x] < self.union_depth[y]: x, y = y, x if self.union_depth[x] == self.union_depth[y]: self.union_depth[x] += 1 self.union_root[x] += self.union_root[y] self.union_root[y] = x self.e_num[x]+=self.e_num[y]+1 def size(self,x): return -self.union_root[self.find(x)] def same(self,x,y): return self.find(x)==self.find(y) def edge(self,x): return self.e_num[self.find(x)] n=int(input()) x=[0] y=[0] t=[0] for i in range(n): a,b,c=map(int,input().split()) x.append(a) y.append(b) t.append(c) def cost(i,j): if t[i]==t[j]: return (x[i]-x[j])**2+(y[i]-y[j])**2 else: ri2 = x[i] ** 2 + y[i] ** 2 rj2 = x[j] ** 2 + y[j] ** 2 def f(s): if s<0:return 0 if ri2+rj2-s<0:return 1 return (ri2+rj2-s)**2<=4*ri2*rj2 ng=int(abs(ri2**0.5-rj2**0.5)**2)-10 ok=ng+20 while ok-ng>1: mid=(ok+ng)//2 if f(mid):ok=mid else:ng=mid return ok e=[] for i in range(1,n+1): for j in range(i+1,n+1): c=cost(i,j) e.append((i,j,c)) e.sort(key=lambda x:x[2]) uf=unionfind(n+4) for i,j,c in e: uf.unite(i,j) if uf.same(1,n): print(c) exit()