結果

問題 No.5007 Steiner Space Travel
ユーザー titan23titan23
提出日時 2022-10-06 14:13:04
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,809 bytes
コンパイル時間 238 ms
実行使用メモリ 85,868 KB
スコア 2,769,211
最終ジャッジ日時 2022-10-06 14:13:42
合計ジャッジ時間 35,454 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 956 ms
85,436 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 953 ms
85,056 KB
testcase_09 TLE -
testcase_10 AC 974 ms
85,024 KB
testcase_11 AC 961 ms
85,868 KB
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 955 ms
85,204 KB
testcase_15 AC 938 ms
84,880 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 AC 958 ms
85,184 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 AC 949 ms
85,500 KB
testcase_23 TLE -
testcase_24 AC 983 ms
85,180 KB
testcase_25 AC 939 ms
85,324 KB
testcase_26 TLE -
testcase_27 AC 972 ms
85,152 KB
testcase_28 AC 929 ms
85,468 KB
testcase_29 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import random
import time
start = time.time()
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

class KMeans:

  def __init__(self, n_clusters, max_iter = 10, random_seed = 0):
    self.n_clusters = n_clusters
    self.max_iter = max_iter

  def fit(self, X):
    self.labels_ = []
    now = 0
    while len(self.labels_) < len(X):
      self.labels_.append(now)
      now += 1
      now %= self.n_clusters
    random.shuffle(self.labels_)
    labels_prev = [0]*len(X)
    count = 0
    self.cluster_centers_ = [(0, 0)] * self.n_clusters

    while (not (self.labels_ == labels_prev) and count < self.max_iter):
      syuukei = [[] for _ in range(self.n_clusters)]
      for i in range(len(X)):
        syuukei[self.labels_[i]].append(X[i])
      for i,l in enumerate(syuukei):
        if l:
          x, y = sum(x for x,y in l)//len(l), sum(y for x,y in l)//len(l)
        else:
          x, y = random.randint(0, 1000), random.randint(0, 1000)
        self.cluster_centers_[i] = (x, y)

      labels_prev = self.labels_[:]
      for i in range(len(X)):
        dist = 10**18
        for j in range(self.n_clusters):
          tmp = (X[i][0] - self.cluster_centers_[j][0])**2 + (X[i][1] - self.cluster_centers_[j][1])**2
          if tmp < dist:
            dist = tmp
            self.labels_[i] = j
      count += 1


def main():
  model = KMeans(8)
  model.fit(AB)
  labels = model.labels_
  centers = model.cluster_centers_


  def calc(ans):
    def dist(a, b):
      return (a[0]-b[0])**2+(a[1]-b[1])**2
    score = 0
    for i in range(len(ans)-1):
      type_pre, indx_pre = ans[i]
      type_now, indx_now = ans[i+1]
      if type_pre == 1 and type_now == 1:
        score += 25*dist(AB[indx_pre], AB[indx_now])
      if type_pre == 1 and type_now == 2:
        score +=  5*dist(AB[indx_pre], centers[indx_now])
      if type_pre == 2 and type_now == 1:
        score +=  5*dist(centers[indx_pre], AB[indx_now])
      if type_pre == 2 and type_now == 2:
        score +=  1*dist(centers[indx_pre], centers[indx_now])
    return score

  vest = 1<<30
  ANS = []
  L = sorted(enumerate(labels), key=lambda x: x[1])
  for p in itertools.permutations(range(L[-1][1]+1)):
    ans = [(1, 0)]
    for i in p:
      ans.append((2, i))
      for j, cluster_number in L:
        if cluster_number == i:
          ans.append((1, j))
          ans.append((2, i))
    ans.append((1, 0))
    score = calc(ans)
    if score < vest:
      vest = score
      ANS = ans[:]
  return vest, ANS, centers

ANS = []
CENTERS = []
vest = 1<<30
while time.time() - start < 0.7:
  tmp, ans, centers = main()
  if tmp < vest:
      vest = tmp
      ANS = ans[:]
      CENTERS = centers

for c,d in CENTERS:
  print(c, d)
print(len(ANS))
for a,b in ANS:
  print(a, b+1)
0