結果

問題 No.5007 Steiner Space Travel
ユーザー brthyyjpbrthyyjp
提出日時 2023-04-24 22:58:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 981 ms / 1,000 ms
コード長 2,841 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 80,152 KB
スコア 6,908,974
最終ジャッジ日時 2023-04-24 22:59:24
合計ジャッジ時間 32,610 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 978 ms
79,660 KB
testcase_01 AC 981 ms
80,152 KB
testcase_02 AC 978 ms
79,680 KB
testcase_03 AC 978 ms
79,460 KB
testcase_04 AC 979 ms
79,496 KB
testcase_05 AC 978 ms
79,456 KB
testcase_06 AC 978 ms
79,764 KB
testcase_07 AC 977 ms
79,724 KB
testcase_08 AC 978 ms
79,508 KB
testcase_09 AC 977 ms
79,428 KB
testcase_10 AC 978 ms
79,652 KB
testcase_11 AC 980 ms
79,372 KB
testcase_12 AC 977 ms
79,768 KB
testcase_13 AC 975 ms
79,604 KB
testcase_14 AC 977 ms
79,320 KB
testcase_15 AC 976 ms
79,600 KB
testcase_16 AC 977 ms
79,776 KB
testcase_17 AC 979 ms
79,644 KB
testcase_18 AC 978 ms
79,732 KB
testcase_19 AC 977 ms
79,596 KB
testcase_20 AC 979 ms
79,164 KB
testcase_21 AC 975 ms
79,448 KB
testcase_22 AC 979 ms
79,604 KB
testcase_23 AC 979 ms
79,608 KB
testcase_24 AC 980 ms
79,504 KB
testcase_25 AC 977 ms
79,336 KB
testcase_26 AC 978 ms
79,624 KB
testcase_27 AC 980 ms
79,636 KB
testcase_28 AC 977 ms
79,704 KB
testcase_29 AC 978 ms
79,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from time import time
start_time = time()

import sys
import io
import os
input = sys.stdin.readline

import random, math

random.seed(42)
alpha = 5
mx = 1000
INF = float('inf')

limit = 700

n, m = map(int, input().split())
XY = []
for i in range(n):
    x, y = map(int, input().split())
    XY.append((x, y))

CD = []
for i in range(m):
    c = random.randint(0, mx)
    d = random.randint(0, mx)
    CD.append((c, d))

XY += CD

def calc_energy(i, j):
    xi, yi = XY[i]
    xj, yj = XY[j]
    d2 = (xi-xj)**2+(yi-yj)**2
    if 0 <= i < n and 0 <= j < n:    
        return (alpha**2)*d2
    elif n <= i < n+m and n <= j < n+m:
        return d2
    else:
        return alpha*d2

g = [[] for i in range(n+m)]
dist_table = [[INF]*(n+m) for i in range(n+m)]
for i in range(n+m):
    for j in range(n+m):
        if i == j:
            continue
        x1, y1 = XY[j]
        dist =  calc_energy(i, j)
        g[i].append((dist, j))
        dist_table[i][j] = dist


def calc_dist(path, dist_table):
    res = 0
    for v, nv in zip(path, path[1:]):
        res += dist_table[v][nv]
    return res

def nearest_neighbor(s, g):
    n = len(g)
    nonvisit = set(range(n))
    path = []
    path.append(s)
    nonvisit.remove(s)
    while nonvisit:
        min_dist = INF
        nx = -1
        for d, v in g[path[-1]]:
            if not v in nonvisit:
                continue
            if d < min_dist:
                min_dist = d
                nx = v
        path.append(nx)
        nonvisit.remove(nx)
    return path+[s]


def two_opt_with_annealing(path, dist_table, Ts, Te, time_limit):
    n = len(path)
    while True:
        now_time = time()
        if now_time - start_time > time_limit:
            return path
        for i in range(n-3):
            p1 = path[i]
            p2 = path[i+1]
            p_dist = dist_table[p1][p2]
            for j in range(i+2, n-1):
                q1 = path[j]
                q2 = path[j+1]
                q_dist = dist_table[q1][q2]
                cur_dist = p_dist + q_dist
                new_dist = dist_table[p1][q1]+dist_table[p2][q2]
                diff = new_dist-cur_dist
                temp = Ts+(Te-Ts)*(time()-start_time)/time_limit
                prob = math.exp(min(limit, -diff / temp))
                if prob > random.random():
                    sep1, sep2, sep3 = path[:i+1], path[i+1:j+1], path[j+1:]
                    sep2.reverse()
                    path = sep1+sep2+sep3

path = nearest_neighbor(0, g)
path = two_opt_with_annealing(path, dist_table, 50, 10, 0.9)

s = calc_dist(path, dist_table)
score = round(10**9/(1000+math.sqrt(s)))
print(score, file=sys.stderr, flush=True)

for c, d in CD:
    print(c, d)

print(len(path))
for k in range(len(path)):
    r = path[k]
    if 0 <= r < n:
        print(1, r+1)
    else:
        print(2, r-n+1)

0