結果
問題 | No.5007 Steiner Space Travel |
ユーザー | titan23 |
提出日時 | 2022-10-06 13:52:47 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,982 bytes |
コンパイル時間 | 240 ms |
実行使用メモリ | 84,152 KB |
スコア | 0 |
最終ジャッジ日時 | 2022-10-06 13:53:05 |
合計ジャッジ時間 | 16,184 ms |
ジャッジサーバーID (参考情報) |
judge16 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
ソースコード
import numpy as np import itertools import random N, M = map(int, input().split()) AB = [np.array(list(map(int, input().split()))) for _ in range(N)] AB = np.array(AB) 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)): min = -1 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 predict(self, X): dist = ((X[:, :, np.newaxis] - self.cluster_centers_.T[np.newaxis, :, :]) ** 2).sum(axis = 1) labels = dist.argmin(axis = 1) return labels model = KMeans(8) model.fit(AB) labels = model.labels_ centers = model.cluster_centers_ ans = [(1, 0)] L = sorted(enumerate(labels), key=lambda x: x[1]) for i in range(L[-1][1]+1): for j, cluster_number in L: if cluster_number == i: ans.append((1, j)) ans.append((2, i)) ans.append((1, 0)) for c,d in centers: print(c, d) print(len(ans)) for a,b in ans: print(a, b+1)