結果
問題 | No.1283 Extra Fee |
ユーザー |
👑 ![]() |
提出日時 | 2021-02-10 18:50:21 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 809 ms / 2,000 ms |
コード長 | 2,006 bytes |
コンパイル時間 | 396 ms |
コンパイル使用メモリ | 82,708 KB |
実行使用メモリ | 117,028 KB |
最終ジャッジ日時 | 2024-11-16 07:18:18 |
合計ジャッジ時間 | 12,784 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
from heapq import heappop,heappushclass Heap_Point:def __init__(self,x,d):self.d=dself.x=xdef __str__(self):return "(point:{}, dist:{})".format(self.x,self.d)def __repr__(self):return str(self)def __lt__(self,other):return self.d<other.ddef __iter__(self):yield from (self.x,self.d)class Dijkstra_Heap:def __init__(self):self.heap=[]def __str__(self):return str(self.heap)def __repr__(self):return repr(self.heap)def __bool__(self):return bool(self.heap)def push(self,point,dist):heappush(self.heap,Heap_Point(point,dist))def pop(self):assert self.heapreturn heappop(self.heap)def main():import sysfrom heapq import heappop,heappushinput=sys.stdin.readlineN,M=map(int,input().split())inf=float("inf")F=[[1]*(N+1) for _ in range(N+1)]for _ in range(M):h,w,c=map(int,input().split())F[h][w]+=cV=[(1,0),(-1,0),(0,1),(0,-1)]Dist0=[[inf]*(N+1) for __ in range(N+1)]Dist1=[[inf]*(N+1) for __ in range(N+1)]Dist0[1][1]=0H=Dijkstra_Heap()H.push((1,1,0),0)while H:v,d=H.pop()i,j,m=vif (m==0 and d<Dist0[i][j]) or (m==1 and d<Dist1[i][j]):continueif i==j==N:breakfor (a,b) in V:if 1<=i+a<=N and 1<=j+b<=N:c=d+F[i+a][j+b]if m==0 and c<Dist0[i+a][j+b]:Dist0[i+a][j+b]=cH.push((i+a,j+b,0),c)if m==1 and c<Dist1[i+a][j+b]:Dist1[i+a][j+b]=cH.push((i+a,j+b,1),c)if m==0 and d+1<Dist1[i+a][j+b]:Dist1[i+a][j+b]=d+1H.push((i+a,j+b,1),d+1)print(min(Dist0[N][N],Dist1[N][N]))if __name__=="__main__":main()