結果

問題 No.5007 Steiner Space Travel
ユーザー shotoyooshotoyoo
提出日時 2022-07-30 15:26:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 144 ms / 1,000 ms
コード長 3,052 bytes
コンパイル時間 257 ms
実行使用メモリ 83,400 KB
スコア 6,448,541
最終ジャッジ日時 2022-07-30 15:26:33
合計ジャッジ時間 6,337 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
82,748 KB
testcase_01 AC 130 ms
82,960 KB
testcase_02 AC 131 ms
83,104 KB
testcase_03 AC 131 ms
83,168 KB
testcase_04 AC 134 ms
82,984 KB
testcase_05 AC 133 ms
82,952 KB
testcase_06 AC 133 ms
83,008 KB
testcase_07 AC 134 ms
83,116 KB
testcase_08 AC 133 ms
82,896 KB
testcase_09 AC 138 ms
82,852 KB
testcase_10 AC 144 ms
82,896 KB
testcase_11 AC 132 ms
82,960 KB
testcase_12 AC 129 ms
83,080 KB
testcase_13 AC 132 ms
82,968 KB
testcase_14 AC 133 ms
82,980 KB
testcase_15 AC 129 ms
82,952 KB
testcase_16 AC 131 ms
82,804 KB
testcase_17 AC 132 ms
82,816 KB
testcase_18 AC 129 ms
82,780 KB
testcase_19 AC 129 ms
83,168 KB
testcase_20 AC 135 ms
83,036 KB
testcase_21 AC 138 ms
83,080 KB
testcase_22 AC 140 ms
83,140 KB
testcase_23 AC 132 ms
82,876 KB
testcase_24 AC 132 ms
83,060 KB
testcase_25 AC 132 ms
83,400 KB
testcase_26 AC 133 ms
82,812 KB
testcase_27 AC 135 ms
82,852 KB
testcase_28 AC 139 ms
82,952 KB
testcase_29 AC 135 ms
82,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, random
input = lambda : sys.stdin.readline().rstrip()


write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x))
debug = lambda x: sys.stderr.write(x+"\n")
YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO); INF=10**18
LI = lambda : list(map(int, input().split())); II=lambda : int(input())
def debug(_l_):
    for s in _l_.split():
        print(f"{s}={eval(s)}", end=" ")
    print()
def dlist(*l, fill=0):
    if len(l)==1:
        return [fill]*l[0]
    ll = l[1:]
    return [dlist(*ll, fill=fill) for _ in range(l[0])]
import random
def kmean(xy, k):
    n = len(xy)
    # cs = [(random.randint(0,1000), random.randint(0,1000)) for _ in range(k)]
    if k>=n:
        cs = [xy[i%n] for i in range(k)]
        index = [[] for i in range(k)]
        for i in range(n):
            index[i].append(i)
        return cs, index
    nokori = set(range(n))
    i0 = random.randint(0,n-1)
    cs = [xy[i0]]
    nokori.remove(i0)
    for i in range(m-1):
        i1 = max(nokori, key=lambda ii: min((xy[ii][0]-x)**2 + (xy[ii][1]-y)**2 for x,y in cs))
        cs.append(xy[i1])
    for _ in range(10):
        index = [[] for _ in range(k)]
        for i in range(n):
            x,y = xy[i]
            ind = min(range(k), key=lambda j: (x-cs[j][0])**2 + (y-cs[j][1])**2)
            index[ind].append(i)
        ncs = []
        for i in range(k):
            sx = sy = 0
            if not index[i]:
                ncs.append((random.randint(0,1000), random.randint(0,100)))
            else:
                for ind in index[i]:
                    x,y = xy[ind]
                    sx += x
                    sy += y
                ncs.append((int(round(sx/len(index[i]))), int(round(sy/len(index[i])))))
        cs = ncs
    return cs, index
def main(n,m,xy,show=False):
    cs,index = kmean(xy, m)
    from itertools import permutations as ps
    best = (INF, None)
    for inds in ps(range(m)):
        cost = 0
        for i in range(m):
            x,y = cs[inds[i]]
            xx,yy = cs[inds[i-1]]
            cost += (x-xx)**2 + (y-yy)**2
        if cost<best[0]:
            best = (cost, inds)
    order = best[1]
    if show:
        for item in cs:
            print(item[0], item[1])
    for i in range(m):
        if 0 in index[i]:
            for j in range(m):
                if order[j]==i:
                    order = order[j:] + order[:j]
                    break
            break
    cost = 0
    alpha = 5
    ans = []
    for j in order:
        item = index[j]
        item.sort()
        for ind in item:
            if show:
                ans.append((1, ind+1))
                ans.append((2, j+1))
            cost += alpha*((xy[ind][0] - cs[j][0])**2 + (xy[ind][1] - cs[j][1])**2)
    cost += best[0]
    if show:
        ans.append((1,1))
        print(len(ans))
        for item in ans:
            print(item[0], item[1])
    return cost, cs, index
n,m = list(map(int, input().split()))
xy = [LI() for _ in range(n)]
cost, cs, index = main(n,m,xy,show=True)
0