結果
| 問題 |
No.2565 はじめてのおつかい
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-02 15:50:50 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 605 ms / 2,000 ms |
| コード長 | 1,715 bytes |
| コンパイル時間 | 339 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 136,892 KB |
| 最終ジャッジ日時 | 2024-09-26 19:24:15 |
| 合計ジャッジ時間 | 18,992 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 50 |
ソースコード
import sys
#sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10**9 + 7
MOD2 = 998244353
from collections import defaultdict
def ceil(A,B):
return -(-A//B)
import heapq
def dijkstra(edges: "List[List[(cost, to)]]", start_node: int) -> list:
hq = []
heapq.heapify(hq)
# Set start info
dist = [INF] * len(edges)
heapq.heappush(hq, (0, start_node))
dist[start_node] = 0
# dijkstra
while hq:
# ------------------------------------------- タイミング1
min_cost, now = heapq.heappop(hq)
# ------------------------------------------- タイミング2
if min_cost > dist[now]:
continue
for cost, next in edges[now]:
if dist[next] > dist[now] + cost:
dist[next] = dist[now] + cost
heapq.heappush(hq, (dist[next], next))
# --------------------------------------- タイミング3
return dist
def solve():
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LC(): return list(sys.stdin.readline().rstrip())
def IC(): return [int(c) for c in sys.stdin.readline().rstrip()]
def MI(): return map(int, sys.stdin.readline().split())
N,M = MI()
Edge = [[] for _ in range(4*N)]
for i in range(M):
U,V = MI()
U-=1
V-=1
for i in range(4):
Edge[i*N+U].append((1,i*N+V))
Edge[N-2].append((0,2*N-2))
Edge[N-1].append((0,3*N-1))
Edge[2*N-1].append((0,4*N-1))
Edge[3*N-2].append((0,4*N -2))
Dist = dijkstra(Edge,0)
#print(Dist)
if(Dist[3*N]==INF):
print(-1)
else:
print(Dist[3*N])
return
solve()