結果

問題 No.2180 Comprehensive Line Segments
ユーザー MasKoaTSMasKoaTS
提出日時 2022-10-12 21:54:05
言語 PyPy3
(7.3.15)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,097 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 1,088,248 KB
最終ジャッジ日時 2024-11-17 01:19:01
合計ジャッジ時間 68,678 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 330 ms
257,496 KB
testcase_01 MLE -
testcase_02 AC 144 ms
95,216 KB
testcase_03 MLE -
testcase_04 AC 176 ms
89,168 KB
testcase_05 AC 146 ms
88,464 KB
testcase_06 AC 145 ms
88,356 KB
testcase_07 AC 150 ms
88,120 KB
testcase_08 AC 148 ms
88,504 KB
testcase_09 AC 246 ms
98,992 KB
testcase_10 AC 378 ms
102,544 KB
testcase_11 MLE -
testcase_12 MLE -
testcase_13 TLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 TLE -
testcase_17 MLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 AC 253 ms
90,676 KB
testcase_21 MLE -
testcase_22 AC 429 ms
93,260 KB
testcase_23 AC 2,439 ms
222,280 KB
testcase_24 AC 966 ms
114,088 KB
testcase_25 TLE -
testcase_26 MLE -
testcase_27 WA -
testcase_28 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
from fractions import Fraction as frac
input = sys.stdin.readline
INF = 10 ** 9

class Vector2:
	def __init__(self, x: frac, y: frac):
		self.x = x
		self.y = y

	def __eq__(self, other):
		return (self.x == other.x and self.y == other.y)

	def __hash__(self):
		return hash((self.x, self.y))

	def __sub__(self, other):
		return Vector2(other.x - self.x, other.y - self.y)

class Line:
	def __init__(self, a: frac, b:frac, c:frac):
		self.a = a
		self.b = b
		self.c = c

	def __eq__(self, other):
		return (self.a == other.a and self.b == other.b and self.c == other.c)

	def __hash__(self):
		return hash((self.a, self.b, self.c))

def normalize_vector(v: Vector2) -> Vector2:
	assert(v.x != 0 or v.y != 0)
	norm = v.x ** 2 + v.y ** 2
	return Vector2(v.x * abs(v.x) / norm, v.y * abs(v.y) / norm)

def calc_line(one: Vector2, other: Vector2) -> Line:
	x1 = one.x;  y1 = one.y
	x2 = other.x;  y2 = other.y
	if(x1 == x2):
		return Line(1, 0, x1)
	a = (y1 - y2) / (x1 - x2)
	c = y1 - a * x1
	return Line(-a, 1, c)

def calc_intersection(one: Line, other: Line) -> Vector2:
	p = one.a * other.b - other.a * one.b
	if(p == 0):
		return None
	q = other.b * one.c - one.b * other.c
	x = q / p
	y = (other.c - other.a * x) / other.b if(one.b == 0) else (one.c - one.a * x) / one.b
	return Vector2(x, y)


"""
Main Code
"""

# 入力
N = int(input())
P = [Vector2(*map(frac, input().split())) for _ in [0] * N]

# 点が1個のときは必ず答え1
if(N == 1):
	print(1)
	exit(0)

# 各点に番号付け
ph = {}
pt_id = 0
for p in P:
	ph[p] = pt_id
	pt_id += 1

# 有り得る直線を調べて番号付け
ln_id = 0
lh = {}
for i in range(N - 1):
	for j in range(i + 1, N):
		p1, p2 = P[i], P[j]
		l = calc_line(p1, p2)
		if(l in lh):
			continue
		lh[l] = ln_id
		ln_id += 1

# 有り得る交点を調べて番号付け
lis = list(lh.keys())
for i in range(ln_id - 1):
	for j in range(i + 1, ln_id):
		l1, l2 = lis[i], lis[j]
		p = calc_intersection(l1, l2)
		if(p is None or p in ph):
			continue
		P.append(p)
		ph[p] = pt_id
		pt_id += 1

# 任意の2点について、2点から構成されるベクトルを調べて正規化
vec_lis = [[None]*pt_id for _ in [0]*pt_id]
for i in range(pt_id - 1):
	for j in range(i + 1, pt_id):
		if(calc_line(P[i],P[j]) not in lh):
			continue
		vec_lis[i][j] = normalize_vector(P[j] - P[i])
		vec_lis[j][i] = normalize_vector(P[i] - P[j])

# グラフ探索
dp = [[[INF]*pt_id for _ in [0]*pt_id] for _ in [0]*(1 << N)]
que = deque([])
for i in range(N):
	que.append((0, 1 << i, i, i))
	dp[1 << i][i][i] = 0
goal = (1 << N) - 1
ans = INF
while(que):
	c, b, lv, v = que.popleft()
	if(c > dp[b][lv][v]):
		continue
	if(b == goal):
		ans = c
		break
	for nv in range(pt_id):
		if(v == nv or min(v, nv) >= N or (vec_lis[v][nv] is None)):
			continue
		nb = b | (1 << nv) if(nv < N) else b
		nc = c
		if(lv == v or vec_lis[lv][v] != vec_lis[v][nv]):
			nc += 1
		if(nc >= dp[nb][v][nv]):
			continue
		dp[nb][v][nv] = nc
		if(nc == c):
			que.appendleft((nc, nb, v, nv))
		else:
			que.append((nc, nb, v, nv))

print(ans)
0