結果

問題 No.5007 Steiner Space Travel
ユーザー shotoyooshotoyoo
提出日時 2022-07-30 15:59:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,790 bytes
コンパイル時間 267 ms
実行使用メモリ 83,936 KB
スコア 0
最終ジャッジ日時 2022-07-30 15:59:21
合計ジャッジ時間 16,236 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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

### 最小全域木 クラスカル法 kruskal
# 連結成分を持っておくために必要
class UnionFindTree:
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        
    def root(self, i):
        inter = set()
        while self.parent[i]>=0:
            inter.add(i)
            i = self.parent[i]
        r = i
        for i in inter:
            self.parent[i] = r
        return r

    def connect(self, i, j):
        """連結操作が実行されたときTrueを返す
        """
        if i==j:
            return False
        ri = self.root(i)
        rj = self.root(j)
        if ri==rj:
            return False
        si = -self.parent[ri]
        sj = -self.parent[rj]
        if si<sj:
            self.parent[ri] = rj
            self.parent[rj] -= si
        else:
            self.parent[rj] = ri
            self.parent[ri] -= sj
        return True
            
    def size(self, i):
        return -self.parent[self.root(i)]


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(50):
        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 = []
    def dist(cur,u):
        cx,cy = xy[cur] if cur<n else cs[cur-n]
        ux,uy = xy[u] if u<n else cs[u-n]
        return ((cx-ux)**2 + (cy-uy)**2)
    for j in order:
        item = index[j]
        if not item:
            continue
        item.sort()
        cur = item[0]
        nokori = set(item)
        nokori.remove(cur)
        ans.append((1, cur+1))
        while nokori:
            u = min(nokori, key=lambda u: alpha*(dist(cur,u)))
            d = dist(cur,u)
            if alpha*d < dist(cur,n+j) + dist(n+j,u):
                ans.append((1, u))
                cost += alpha*alpha*d
            else:
                ans.append((2, j+1))
                ans.append((1, u))
                cost += (dist(cur, n+j) + dist(n+j, u)) * alpha
            cur = u
            nokori.remove(u)
        ans.append((2, j+1))
#         for ind in item:
#             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]
    ans.append((1,1))
    if show:
        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