結果
問題 | No.5007 Steiner Space Travel |
ユーザー | ー葵ーAoi |
提出日時 | 2022-07-30 14:32:24 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 51 ms / 1,000 ms |
コード長 | 1,516 bytes |
コンパイル時間 | 81 ms |
実行使用メモリ | 8,484 KB |
スコア | 6,309,481 |
最終ジャッジ日時 | 2022-07-30 14:32:28 |
合計ジャッジ時間 | 3,094 ms |
ジャッジサーバーID (参考情報) |
judge10 / judge11 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
8,376 KB |
testcase_01 | AC | 45 ms
8,284 KB |
testcase_02 | AC | 39 ms
8,416 KB |
testcase_03 | AC | 42 ms
8,444 KB |
testcase_04 | AC | 44 ms
8,472 KB |
testcase_05 | AC | 42 ms
8,360 KB |
testcase_06 | AC | 45 ms
8,316 KB |
testcase_07 | AC | 48 ms
8,284 KB |
testcase_08 | AC | 45 ms
8,276 KB |
testcase_09 | AC | 41 ms
8,232 KB |
testcase_10 | AC | 42 ms
8,144 KB |
testcase_11 | AC | 42 ms
8,292 KB |
testcase_12 | AC | 45 ms
8,268 KB |
testcase_13 | AC | 44 ms
8,180 KB |
testcase_14 | AC | 48 ms
8,420 KB |
testcase_15 | AC | 42 ms
8,300 KB |
testcase_16 | AC | 42 ms
8,136 KB |
testcase_17 | AC | 50 ms
8,148 KB |
testcase_18 | AC | 42 ms
8,484 KB |
testcase_19 | AC | 45 ms
8,268 KB |
testcase_20 | AC | 39 ms
8,308 KB |
testcase_21 | AC | 44 ms
8,460 KB |
testcase_22 | AC | 42 ms
8,152 KB |
testcase_23 | AC | 51 ms
8,312 KB |
testcase_24 | AC | 44 ms
8,276 KB |
testcase_25 | AC | 44 ms
8,356 KB |
testcase_26 | AC | 47 ms
8,388 KB |
testcase_27 | AC | 41 ms
8,344 KB |
testcase_28 | AC | 44 ms
8,280 KB |
testcase_29 | AC | 45 ms
8,284 KB |
ソースコード
def greedy(cities,current_city,dist): N = len(cities) unvisited_cities = set(range(0, N)) tour = [current_city] unvisited_cities.remove(current_city) while unvisited_cities: next_city = min(unvisited_cities, key=lambda city: dist[current_city][city]) unvisited_cities.remove(next_city) tour.append(next_city) current_city = next_city return tour,dist def two_opt(tour, dist): N = len(tour) while True: count = 0 for i in range(N-2): for j in range(i+2, N): l1 = dist[tour[i]][tour[i + 1]] l2 = dist[tour[j]][tour[(j + 1) % N]] l3 = dist[tour[i]][tour[j]] l4 = dist[tour[i + 1]][tour[(j + 1) % N]] if l1 + l2 > l3 + l4: new_tour = tour[i+1 : j+1] tour[i+1 : j+1] = new_tour[::-1] count += 1 if count == 0: break return tour # ==================== N, M = map(int, input().split()) planet = [tuple(map(int, input().split())) for _ in range(N)] dist = [[0 for _ in range(N)] for _ in range(N)] for i in range(N): for j in range(N): p1 = planet[i] p2 = planet[j] D = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2 dist[i][j] = D tour, dis = greedy(planet, 0, dist) tour = two_opt(tour, dis) for _ in range(M):print(*planet[16]) print(len(tour)+1) for i in range(len(tour)):print(1, tour[i]+1) print(1, 1)