結果
問題 | No.1283 Extra Fee |
ユーザー |
![]() |
提出日時 | 2020-11-07 23:29:26 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,597 bytes |
コンパイル時間 | 305 ms |
コンパイル使用メモリ | 82,080 KB |
実行使用メモリ | 203,816 KB |
最終ジャッジ日時 | 2024-07-22 14:56:08 |
合計ジャッジ時間 | 30,189 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 TLE * 4 |
ソースコード
from heapq import heapify, heappop, heappush N,M = map(int,input().split()); INF = float("inf") dic = {} for i in range(M): h,w,c = map(int,input().split()) h-=1;w-=1 dic[h*N+w] = c #print(dic) G = [[] for _ in range(N*N)] dxdy = [(0,1),(0,-1),(1,0),(-1,0)] for i in range(N): for j in range(N): now = i*N + j for k in range(4): ni = i+dxdy[k][0] nj = j+dxdy[k][1] if 0<=ni<N and 0<=nj<N: nxt = ni*N+nj if nxt in dic: G[now].append((1+dic[nxt],nxt)) else: G[now].append((1,nxt)) #print(G) def dijkstra_heap2(s,G): #S:start, V: node, E: Edge, G: Graph V = len(G) d = [[INF,INF] for _ in range(V)] #d[i][j];j=0権利を行使していないj=1どこかで権利を行使済み d[s][0] = 0; d[s][1] = 0 PQ = [] heappush(PQ,(0,s,0)) #コスト0,初期点s,権利行使していない while PQ: c,v,flag = heappop(PQ) if flag == 0 and d[v][0] < c: continue if flag == 1 and d[v][1] < c: continue d[v][flag] = c for cost,u in G[v]: if flag == 0: if d[u][0] <= cost + d[v][0]: pass else: d[u][0] = cost + d[v][0] heappush(PQ,(d[u][0], u, 0)) if d[u][1] <= d[v][0]: pass else: d[u][1] = 1 + d[v][0] heappush(PQ,(d[u][1], u, 1)) else: #flag == 1 if d[u][1] <= cost + d[v][1]: pass else: d[u][1] = cost + d[v][1] heappush(PQ,(d[u][1], u, 1)) return d D = dijkstra_heap2(0,G) #print(D) ans = min(D[-1]) print(ans)