結果

問題 No.1565 Union
ユーザー moharan627moharan627
提出日時 2021-06-26 15:25:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,004 ms / 2,000 ms
コード長 1,367 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 77,568 KB
最終ジャッジ日時 2023-12-14 23:22:51
合計ジャッジ時間 13,255 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,240 KB
testcase_01 AC 29 ms
10,240 KB
testcase_02 AC 29 ms
10,240 KB
testcase_03 AC 29 ms
10,240 KB
testcase_04 AC 29 ms
10,240 KB
testcase_05 AC 29 ms
10,240 KB
testcase_06 AC 29 ms
10,240 KB
testcase_07 AC 29 ms
10,240 KB
testcase_08 AC 30 ms
10,240 KB
testcase_09 AC 29 ms
10,240 KB
testcase_10 AC 256 ms
35,840 KB
testcase_11 AC 429 ms
51,584 KB
testcase_12 AC 437 ms
51,072 KB
testcase_13 AC 163 ms
23,936 KB
testcase_14 AC 548 ms
57,600 KB
testcase_15 AC 1,004 ms
72,960 KB
testcase_16 AC 492 ms
70,784 KB
testcase_17 AC 991 ms
72,960 KB
testcase_18 AC 1,004 ms
72,960 KB
testcase_19 AC 980 ms
72,960 KB
testcase_20 AC 427 ms
77,568 KB
testcase_21 AC 437 ms
77,568 KB
testcase_22 AC 430 ms
77,568 KB
testcase_23 AC 443 ms
77,568 KB
testcase_24 AC 448 ms
77,568 KB
testcase_25 AC 519 ms
77,568 KB
testcase_26 AC 512 ms
77,568 KB
testcase_27 AC 520 ms
77,568 KB
testcase_28 AC 512 ms
77,568 KB
testcase_29 AC 504 ms
77,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#sys.setrecursionlimit(10 ** 6)
INF = float('inf')
#10**20に変えるのもあり
MOD = 10**9 + 7
MOD2 = 998244353
def solve():
    def II(): return int(sys.stdin.readline())
    def LI(): return list(map(int, sys.stdin.readline().split()))
    def LC(): return list(input())
    def IC(): return [int(c) for c in input()]
    def MI(): return map(int, sys.stdin.readline().split())
    N,M = MI()
    Graph = [[] for n in range(N + 1)]
    for i in range(M):
        A, B = MI()
        Graph[A].append((B,1))
        Graph[B].append((A,1))
    from heapq import heappush, heappop
    def dijkstra(s, n):  # (始点, ノード数)
        dist = [INF] * n
        hq = [(0, s)]  # (distance, node)
        dist[s] = 0
        seen = [False] * n  # ノードが確定済みかどうか
        while hq:
            v = heappop(hq)[1]  # ノードを pop する
            if seen[v]:
                continue
            seen[v] = True
            for to, cost in Graph[v]:  # ノード v に隣接しているノードに対して
                if seen[to] == False and dist[v] + cost < dist[to]:
                    dist[to] = dist[v] + cost
                    heappush(hq, (dist[to], to))
        return dist
    Dist = dijkstra(1, N+1)
    #print(Dist)
    if(Dist[N] == INF):
        print(-1)
    else:
        print(Dist[N])
    return
solve()
0