結果
| 問題 | No.1995 CHIKA Road |
| コンテスト | |
| ユーザー |
とりゐ
|
| 提出日時 | 2022-07-01 21:35:00 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 971 ms / 2,000 ms |
| コード長 | 739 bytes |
| 記録 | |
| コンパイル時間 | 154 ms |
| コンパイル使用メモリ | 82,296 KB |
| 実行使用メモリ | 193,852 KB |
| 最終ジャッジ日時 | 2024-11-26 04:10:36 |
| 合計ジャッジ時間 | 14,963 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
from heapq import heappush, heappop
INF=10**18
def dijkstra():
dist=defaultdict(lambda:INF)
hq=[(0,1)]
dist[1]=0
seen=set()
while hq:
w,v=heappop(hq)
if dist[v]<w:
continue
seen.add(v)
for to,cost in edge[v]:
if to not in seen and dist[v]+cost<dist[to]:
dist[to]=dist[v]+cost
heappush(hq,(dist[to],to))
return dist
from collections import defaultdict
edge=defaultdict(list)
n,m=map(int,input().split())
town=set([1,n])
for _ in range(m):
a,b=map(int,input().split())
town.add(a)
town.add(b)
edge[a].append((b,2*b-2*a-1))
town=sorted(list(town))
k=len(town)
for i in range(k-1):
edge[town[i]].append((town[i+1],2*(town[i+1]-town[i])))
#print(edge)
d=dijkstra()
print(d[n])
とりゐ