結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー Theta
提出日時 2023-01-27 18:24:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,568 ms / 2,000 ms
コード長 1,281 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 194,500 KB
最終ジャッジ日時 2024-06-28 03:42:15
合計ジャッジ時間 35,662 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush
from math import inf, sqrt
from operator import ne


class Point:
    def __init__(self, x, y) -> None:
        self.x = x
        self.y = y

    def distance_to(self, __o: object):
        if not isinstance(__o, Point):
            raise ValueError
        return sqrt((self.x - __o.x)**2 + (self.y - __o.y)**2)


def main():
    N, M = map(int, input().split())
    X, Y = map(int, input().split())
    X -= 1
    Y -= 1
    nodes = [Point(*map(int, input().split())) for _ in range(N)]
    weights = [{} for _ in range(N)]
    for _ in range(M):
        start, end = map(int, input().split())
        weights[start-1][end-1] = nodes[start-1].distance_to(nodes[end-1])
        weights[end-1][start-1] = weights[start-1][end-1]

    searched = []
    heappush(searched, (0, X))
    costs = [inf for _ in range(N)]
    decided = set()

    while searched:
        current_cost, next_node_idx = heappop(searched)
        if next_node_idx in decided:
            continue
        costs[next_node_idx] = current_cost
        decided.add(next_node_idx)
        for dest_idx, distance in weights[next_node_idx].items():
            heappush(searched, (current_cost + distance, dest_idx))

    print(costs[Y])


if __name__ == "__main__":
    main()
0