結果

問題 No.2354 Poor Sight in Winter
ユーザー mkawa2mkawa2
提出日時 2023-06-16 22:56:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 971 ms / 2,000 ms
コード長 2,782 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 86,816 KB
実行使用メモリ 224,904 KB
最終ジャッジ日時 2023-09-06 21:36:43
合計ジャッジ時間 10,972 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,716 KB
testcase_01 AC 83 ms
71,532 KB
testcase_02 AC 86 ms
71,472 KB
testcase_03 AC 79 ms
71,568 KB
testcase_04 AC 82 ms
71,720 KB
testcase_05 AC 85 ms
71,848 KB
testcase_06 AC 83 ms
71,468 KB
testcase_07 AC 85 ms
71,624 KB
testcase_08 AC 84 ms
71,716 KB
testcase_09 AC 91 ms
76,412 KB
testcase_10 AC 90 ms
76,868 KB
testcase_11 AC 698 ms
205,408 KB
testcase_12 AC 665 ms
80,280 KB
testcase_13 AC 585 ms
97,492 KB
testcase_14 AC 707 ms
103,228 KB
testcase_15 AC 971 ms
224,904 KB
testcase_16 AC 755 ms
116,696 KB
testcase_17 AC 605 ms
100,216 KB
testcase_18 AC 267 ms
80,296 KB
testcase_19 AC 554 ms
110,484 KB
testcase_20 AC 465 ms
145,004 KB
testcase_21 AC 172 ms
79,388 KB
testcase_22 AC 257 ms
82,988 KB
testcase_23 AC 292 ms
101,048 KB
testcase_24 AC 648 ms
128,876 KB
testcase_25 AC 309 ms
110,192 KB
testcase_26 AC 258 ms
99,604 KB
testcase_27 AC 127 ms
78,312 KB
testcase_28 AC 170 ms
78,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# md = 10**9+7
md = 998244353

from heapq import *
from collections import defaultdict
class UnionFind:
    def __init__(self, n):
        self._tree = [-1]*n
        # number of connected component
        self.cnt = n

    def root(self, u):
        stack = []
        while self._tree[u] >= 0:
            stack.append(u)
            u = self._tree[u]
        for v in stack: self._tree[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        u, v = self.root(u), self.root(v)
        if u == v: return False
        if self._tree[u] > self._tree[v]: u, v = v, u
        self._tree[u] += self._tree[v]
        self._tree[v] = u
        self.cnt -= 1
        return True

    # size of connected component
    def size(self, u):
        return -self._tree[self.root(u)]

def binary_search(l, r, minimize):
    if minimize: l -= 1
    else: r += 1
    while l+1 < r:
        m = (l+r)//2
        if ok(m) ^ minimize: l = m
        else: r = m
    if minimize: return r
    return l

def ok(m):
    uf=UnionFind(n)
    for i in range(n):
        for j in range(i):
            if man[i][j]<=m:uf.merge(i,j)

    if uf.same(0,1):return True

    cost=defaultdict(lambda :inf)
    roots=[uf.root(i) for i in range(n)]
    uu=set(roots)
    for i in range(n):
        u=roots[i]
        for j in range(i):
            v=roots[j]
            cost[u,v]=cost[v,u]=min(cost[u,v],(man[i][j]+m-1)//m-1)
    dist=defaultdict(lambda :inf)
    u=roots[0]
    dist[u]=0
    hp=[(0,u)]
    while hp:
        d,u=heappop(hp)
        if d>k:return False
        if u==roots[1]:return True
        for v in uu:
            c=cost[u,v]
            nd=d+c
            if nd>=dist[v]:continue
            dist[v]=nd
            heappush(hp,(nd,v))
    return False

n,k=LI()
sx,sy,gx,gy=LI()
xy=[[sx,sy],[gx,gy]]+LLI(n)

n+=2
man=[[0]*n for _ in range(n)]
for i,(x,y) in enumerate(xy):
    for j,(s,t) in enumerate(xy[:i]):
        man[i][j]=man[j][i]=abs(x-s)+abs(y-t)

ans=binary_search(1,200000,True)
print(ans)
0