結果
問題 | No.5007 Steiner Space Travel |
ユーザー | Edomonndo365 |
提出日時 | 2023-04-25 14:38:38 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 947 ms / 1,000 ms |
コード長 | 3,520 bytes |
コンパイル時間 | 729 ms |
コンパイル使用メモリ | 86,948 KB |
実行使用メモリ | 82,432 KB |
スコア | 7,599,387 |
最終ジャッジ日時 | 2023-04-25 14:39:13 |
合計ジャッジ時間 | 31,708 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 942 ms
80,552 KB |
testcase_01 | AC | 941 ms
82,116 KB |
testcase_02 | AC | 941 ms
81,128 KB |
testcase_03 | AC | 942 ms
80,996 KB |
testcase_04 | AC | 943 ms
81,960 KB |
testcase_05 | AC | 943 ms
81,936 KB |
testcase_06 | AC | 946 ms
81,840 KB |
testcase_07 | AC | 945 ms
81,384 KB |
testcase_08 | AC | 942 ms
82,432 KB |
testcase_09 | AC | 940 ms
81,132 KB |
testcase_10 | AC | 945 ms
81,864 KB |
testcase_11 | AC | 944 ms
80,712 KB |
testcase_12 | AC | 941 ms
80,956 KB |
testcase_13 | AC | 943 ms
81,708 KB |
testcase_14 | AC | 942 ms
81,672 KB |
testcase_15 | AC | 941 ms
82,104 KB |
testcase_16 | AC | 947 ms
81,476 KB |
testcase_17 | AC | 942 ms
81,820 KB |
testcase_18 | AC | 947 ms
82,112 KB |
testcase_19 | AC | 941 ms
81,260 KB |
testcase_20 | AC | 944 ms
81,584 KB |
testcase_21 | AC | 943 ms
81,500 KB |
testcase_22 | AC | 945 ms
82,188 KB |
testcase_23 | AC | 945 ms
81,904 KB |
testcase_24 | AC | 946 ms
81,736 KB |
testcase_25 | AC | 941 ms
80,380 KB |
testcase_26 | AC | 941 ms
81,456 KB |
testcase_27 | AC | 943 ms
80,560 KB |
testcase_28 | AC | 942 ms
81,524 KB |
testcase_29 | AC | 945 ms
81,972 KB |
ソースコード
import sys from time import time from random import randrange START = time() INF=10**9 class Node: def __init__(self, isTerminal: bool, idx: int, pos: list): self.isTerminal = isTerminal self.idx = idx self.pos = pos self.dist = INF def __str__(self): return "{} {}".format(2-self.isTerminal, self.idx) def get_time(START): return time() - START def dist(p1,p2): x1,y1=p1 x2,y2=p2 return (x1-x2)**2 + (y1-y2)**2 def calc(idx: int): d = dist(ans[idx].pos, ans[idx+1].pos) if ans[idx].isTerminal and ans[idx+1].isTerminal: a = 25 elif ans[idx].isTerminal or ans[idx+1].isTerminal: a = 5 else: a = 1 return a * d # インプット N,M=map(int,input().split()) Terminals = [list(map(int,input().split())) for _ in range(N)] # 初期解 greedy ans = [Node(True,1,Terminals[0])] candidates = set(range(1,N)) for _ in range(N-1): best_index = -1 best_dist = INF for cand in candidates: d = dist(ans[-1].pos, Terminals[cand]) if d < best_dist: best_dist = d best_index = cand assert best_index!=-1 ans.append(Node(True, best_index+1, Terminals[best_index])) candidates.discard(best_index) ans.append(Node(True,1,Terminals[0])) # 2-optで山登り n = len(ans) loop_cnt1 = 0 while get_time(START) < 0.85: loop_cnt1+=1 v1 = randrange(1,n-1) v2 = randrange(1,n-1) while v1==v2: v2 = randrange(1,n-1) current_score = calc(v1-1) + calc(v1) + calc(v2-1) + calc(v2) ans[v1],ans[v2] = ans[v2],ans[v1] changed_score = calc(v1-1) + calc(v1) + calc(v2-1) + calc(v2) if changed_score < current_score: continue else: ans[v1],ans[v2] = ans[v2],ans[v1] # 宇宙ステーションの座標決め(最初は距離が遠い頂点に置く) candidates = [] for i in range(N): d = dist(ans[i].pos, ans[i+1].pos) candidates.append([i,d]) candidates.sort(key=lambda x:x[1], reverse=1) Stations = [] for i,cand in enumerate(candidates[:M], start=1): idx = cand[0] Stations.append(Node(False, i, ans[idx].pos)) # 宇宙ステーションを同座標の駅の直後に追加していく for station in Stations: for i in range(len(ans)): if ans[i].pos == station.pos: ans.insert(i+1, station) break # 全頂点に対して、宇宙ステーションを追加するかどうかを探索 bestScore = 0 loop_cnt2 = 0 idx=0 while True: if idx >= len(ans)-1: break if not ans[idx].isTerminal and not ans[idx+1].isTerminal: idx += 1 continue if ans[idx].isTerminal and ans[idx+1].isTerminal: a = 25 else: a = 5 cur_dist = a * dist(ans[idx].pos,ans[idx+1].pos) best_dist = cur_dist best_station = station for station in Stations: a1 = 5 if ans[idx].isTerminal else 1 a2 = 5 if ans[idx+1].isTerminal else 1 _dist = a1 * dist(ans[idx].pos, station.pos) + a2 * dist(station.pos, ans[idx+1].pos) if _dist < best_dist: best_dist = _dist best_station = station if best_dist != cur_dist: ans.insert(idx+1, best_station) idx += 2 else: idx += 1 # アウトプット for node in Stations: print(*node.pos) print(len(ans)) for node in ans: print(node) print("Loop1", loop_cnt1, file=sys.stderr) print("Loop2", loop_cnt2, file=sys.stderr) print("time:",get_time(START) * 1000, file=sys.stderr)