結果

問題 No.2179 Planet Traveler
ユーザー taiga0629kyopro
提出日時 2022-09-26 18:10:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 652 ms / 3,000 ms
コード長 1,931 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 82,352 KB
実行使用メモリ 131,088 KB
最終ジャッジ日時 2024-11-30 16:29:35
合計ジャッジ時間 10,823 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #







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()


0