結果

問題 No.5007 Steiner Space Travel
ユーザー Edomonndo365Edomonndo365
提出日時 2023-04-25 14:33:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,193 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 87,348 KB
実行使用メモリ 83,028 KB
スコア 6,551,449
最終ジャッジ日時 2023-04-25 14:33:58
合計ジャッジ時間 34,004 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 996 ms
80,860 KB
testcase_01 AC 999 ms
83,028 KB
testcase_02 AC 998 ms
81,496 KB
testcase_03 TLE -
testcase_04 AC 999 ms
81,436 KB
testcase_05 AC 997 ms
81,648 KB
testcase_06 AC 995 ms
81,540 KB
testcase_07 AC 997 ms
80,700 KB
testcase_08 TLE -
testcase_09 AC 999 ms
80,472 KB
testcase_10 AC 999 ms
80,964 KB
testcase_11 AC 997 ms
81,888 KB
testcase_12 AC 998 ms
82,008 KB
testcase_13 AC 996 ms
82,412 KB
testcase_14 TLE -
testcase_15 AC 997 ms
81,892 KB
testcase_16 AC 999 ms
81,268 KB
testcase_17 AC 996 ms
80,780 KB
testcase_18 AC 995 ms
81,044 KB
testcase_19 AC 998 ms
82,388 KB
testcase_20 AC 995 ms
81,588 KB
testcase_21 AC 996 ms
80,640 KB
testcase_22 AC 996 ms
80,612 KB
testcase_23 AC 994 ms
81,936 KB
testcase_24 AC 998 ms
82,712 KB
testcase_25 TLE -
testcase_26 AC 994 ms
81,588 KB
testcase_27 AC 996 ms
80,700 KB
testcase_28 AC 996 ms
80,920 KB
testcase_29 AC 998 ms
81,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)]

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]))

n = len(ans)
loop_cnt1 = 0
while get_time(START) < 0.9:
    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)
0