結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー Theta
提出日時 2023-01-27 18:22:13
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,331 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 139,004 KB
最終ジャッジ日時 2024-06-28 03:40:13
合計ジャッジ時間 32,229 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31 TLE * 15
権限があれば一括ダウンロードができます

ソースコード

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]
    

    unvisited = set(idx for idx in range(N))
    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