結果

問題 No.168 ものさし
ユーザー rpy3cpprpy3cpp
提出日時 2015-05-21 12:34:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 334 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 11,000 KB
実行使用メモリ 43,880 KB
最終ジャッジ日時 2023-08-25 20:56:46
合計ジャッジ時間 3,662 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
17,100 KB
testcase_01 AC 17 ms
8,760 KB
testcase_02 AC 16 ms
8,720 KB
testcase_03 AC 16 ms
8,724 KB
testcase_04 AC 17 ms
8,836 KB
testcase_05 AC 17 ms
8,652 KB
testcase_06 AC 17 ms
8,668 KB
testcase_07 AC 16 ms
8,768 KB
testcase_08 AC 17 ms
8,712 KB
testcase_09 AC 18 ms
8,348 KB
testcase_10 AC 25 ms
9,616 KB
testcase_11 AC 85 ms
16,048 KB
testcase_12 AC 256 ms
32,304 KB
testcase_13 AC 329 ms
42,120 KB
testcase_14 AC 212 ms
38,280 KB
testcase_15 AC 17 ms
8,824 KB
testcase_16 AC 19 ms
8,264 KB
testcase_17 AC 22 ms
9,060 KB
testcase_18 AC 31 ms
10,408 KB
testcase_19 AC 334 ms
43,376 KB
testcase_20 AC 256 ms
43,880 KB
testcase_21 AC 330 ms
43,336 KB
testcase_22 AC 254 ms
43,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


def read_data():
    N = int(input())
    ps = [list(map(int, input().split())) for i in range(N)]
    return N, ps


def solve(N, ps):
    dist = set_dist(ps)
    tick = 10
    start = 0
    goal = N - 1
    dist_s = [dist[start][v] for v in range(N)]
    dist_g = [dist[goal][v] for v in range(N)]
    pq_s = [(dist_s[v], v) for v in range(1, N)]
    pq_g = [(dist_g[v], v) for v in range(0, N - 1)]
    heapq.heapify(pq_s)
    heapq.heapify(pq_g)
    max_scale2 = 0
    found = False
    while not found:
        d_s, vs = get_next(pq_s, dist_s)
        d_g, vg = get_next(pq_g, dist_g)
        if d_s < d_g:
            if d_s > max_scale2:
                max_scale2 = d_s
            found = process(dist_s, dist_g, pq_s, vs, dist[vs])
        else:
            if d_g > max_scale2:
                max_scale2 = d_g
            found = process(dist_g, dist_s, pq_g, vg, dist[vg])
    return square_round(max_scale2, tick)


def set_dist(ps):
    N = len(ps)
    dist = [[0] * N for v in range(N)]
    for i in range(N-1):
        x0, y0 = ps[i]
        for j in range(i + 1, N):
            x1, y1 = ps[j]
            d = (x0 - x1) ** 2 + (y0 - y1) ** 2
            dist[i][j] = d
            dist[j][i] = d
    return dist


def get_next(pq, dist_x):
    d, v = pq[0]
    while not dist_x[v]:
        heapq.heappop(pq)
        d, v = pq[0]
    return d, v


def process(dist_0, dist_1, pq_0, v0, dist_v0):
    heapq.heappop(pq_0)
    if not dist_1[v0]:
        return True
    dist_0[v0] = 0
    for new_v, (d, new_d) in enumerate(zip(dist_0, dist_v0)):
        if new_d < d:
            heapq.heappush(pq_0, (new_d, new_v))
            dist_0[new_v] = new_d
    return False


def square_round(x_sq, tick):
    x = x_sq ** 0.5
    x_int = (int(x) // tick) * tick
    if x_int ** 2 < x_sq:
        x_int += tick
    return x_int


if __name__ == '__main__':
    N, ps = read_data()
    print(solve(N, ps))
0