結果

問題 No.5007 Steiner Space Travel
ユーザー ー葵ーAoiー葵ーAoi
提出日時 2022-07-30 16:06:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 111 ms / 1,000 ms
コード長 3,307 bytes
コンパイル時間 431 ms
実行使用メモリ 9,688 KB
スコア 7,315,444
最終ジャッジ日時 2022-07-30 16:06:52
合計ジャッジ時間 4,502 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
9,580 KB
testcase_01 AC 97 ms
9,452 KB
testcase_02 AC 79 ms
9,484 KB
testcase_03 AC 88 ms
9,460 KB
testcase_04 AC 87 ms
9,448 KB
testcase_05 AC 91 ms
9,612 KB
testcase_06 AC 94 ms
9,572 KB
testcase_07 AC 86 ms
9,432 KB
testcase_08 AC 87 ms
9,556 KB
testcase_09 AC 81 ms
9,588 KB
testcase_10 AC 81 ms
9,608 KB
testcase_11 AC 89 ms
9,536 KB
testcase_12 AC 83 ms
9,592 KB
testcase_13 AC 87 ms
9,524 KB
testcase_14 AC 92 ms
9,688 KB
testcase_15 AC 83 ms
9,616 KB
testcase_16 AC 83 ms
9,448 KB
testcase_17 AC 85 ms
9,580 KB
testcase_18 AC 82 ms
9,524 KB
testcase_19 AC 84 ms
9,640 KB
testcase_20 AC 87 ms
9,440 KB
testcase_21 AC 89 ms
9,528 KB
testcase_22 AC 92 ms
9,640 KB
testcase_23 AC 111 ms
9,452 KB
testcase_24 AC 88 ms
9,612 KB
testcase_25 AC 84 ms
9,440 KB
testcase_26 AC 86 ms
9,432 KB
testcase_27 AC 91 ms
9,444 KB
testcase_28 AC 89 ms
9,568 KB
testcase_29 AC 84 ms
9,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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
def calc_dist(planet, i, j):
    p1 = planet[i]
    p2 = planet[j]
    D = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
    if p1[2] == p2[2]:
        if p1[2] == 1: # 両方惑星
            D = alpha * alpha * D
        else: # 両方基地
            D = D
    else:
        D = alpha * D
    return D

def calc_dist_from_tour(planet, tour):
    N = len(tour)
    dist = list()
    for i in range(N-1):
        dist.append([calc_dist(planet, tour[i], tour[i+1]), [tour[i], tour[i+1]]])
    return dist

def opt_tour(visit):
    dista = [[0 for _ in range(len(visit))] for _ in range(len(visit))]
    for i in range(len(visit)):
        for j in range(len(visit)):
            dista[i][j] = calc_dist(visit, i, j)
    start_num = -1
    for i in range(len(visit)):
        if visit[i][2] == 1 and visit[i][3] == 0:
            start_num = i
    tour, dis = greedy(visit, start_num, dista)
    tour = two_opt(tour, dis)
    return tour

def score(visit, tour):
    res = 0
    for i in range(len(tour)-1):
        res += calc_dist(visit, tour[i], tour[i + 1])
    res += calc_dist(visit, tour[-1], tour[0])
    return res


def ok(x, y):
    return 0 <= x <= 1000 and 0 <= y <= 1000
# ====================
import math
import random

N, M = map(int, input().split())
planet = [tuple(list(map(int, input().split())) + [1, i]) for i in range(N)]
alpha = 5
dist = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j in range(N):
        dist[i][j] = calc_dist(planet, i, j)
tour, dis = greedy(planet, 0, dist)
tour = two_opt(tour, dis) + [1]
# distanceの長いところの真ん中らへんに宇宙ステーションを作りたい。
# その後もう一回TSPを回して、焼きなましをして、TSPを回して、・・・
long_dist = calc_dist_from_tour(planet, tour)
long_dist.sort(reverse=True)
k = M
stations = []
for i in range(k):
    p1, p2 = long_dist[i][1][0], long_dist[i][1][1]
    stations.append(((planet[p1][0] + planet[p2][0]) // 2, (planet[p1][1] + planet[p2][1]) // 2, 2, i))

visit = stations + planet
tour = opt_tour(visit)

# output
for i in range(M):print(*stations[i][:2])
print(len(visit)+1)
for i in range(len(tour)):
    if visit[tour[i]][2] == 1:
        print(1, visit[tour[i]][3] + 1)
    else:
        print(2, visit[tour[i]][3] + 1)
print(1, 1)
0