1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AOJ 1127 - Building a Space Station 解答例

Posted at

AOJ 1127 - Building a Space Station

Atcoderのなかに答えなかったので、自分用のメモ。
宇宙ステーションの半径と距離の比較にやや工夫がいるだけで、あとはprim heap。

sample.py
import math
import heapq
while True:
    n = int(input())
    if n==0: break
    if n==1: print(0);exit()
    edges = [[] for _ in range(n)]
    coordinates = []
    for _ in range(n):
        x,y,z,r = map(float,input().split())
        coordinates.append((x,y,z,r))
    for i in range(n):
        for j in range(i+1,n):
            dist = math.sqrt((coordinates[i][0]-coordinates[j][0])**2 + (coordinates[i][1]-coordinates[j][1])**2+(coordinates[i][2]-coordinates[j][2])**2)
            if coordinates[i][3] + coordinates[j][3]>=dist:
                dist = 0
            else:
                dist -=coordinates[i][3] + coordinates[j][3]
            edges[i].append((dist,j))
            edges[j].append((dist,i))
    seen = [False]*n
    hq = []
    res = 0
    for e in edges[0]:
        heapq.heappush(hq,e)
    seen[0] = True
    while len(hq):
        minedge = heapq.heappop(hq)
        if seen[minedge[1]]:
            continue
        seen[minedge[1]] = True
        v = minedge[1]
        for e in edges[v]:
            heapq.heappush(hq,e)
        res += minedge[0]
    print(f"{res:.3f}")
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?