結果
問題 |
No.1283 Extra Fee
|
ユーザー |
|
提出日時 | 2020-11-07 02:33:26 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,916 ms / 2,000 ms |
コード長 | 1,195 bytes |
コンパイル時間 | 419 ms |
コンパイル使用メモリ | 82,284 KB |
実行使用メモリ | 384,728 KB |
最終ジャッジ日時 | 2024-11-16 06:57:14 |
合計ジャッジ時間 | 26,334 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
import heapq import sys input=sys.stdin.readline def calc(): n,m=map(int,input().split()) fees=[1]*(n*n) for _ in range(m): y,x,cost=map(int,input().split()) fees[n*(y-1)+x-1]+=cost g=[[] for _ in range(2*n*n)] for y in range(n): for x in range(n): for dx,dy in [(0,1),(0,-1),(1,0),(-1,0)]: if 0<=x+dx<=n-1 and 0<=y+dy<=n-1: u=n*y+x v=n*(y+dy)+x+dx fee=fees[n*(y+dy)+x+dx] if fee!=1: g[u].append([v,fee]) g[u].append([v+n*n,1]) g[u+n*n].append([v+n*n,fee]) else: g[u].append([v,1]) g[u+n*n].append([v+n*n,1]) hq = [(0, 0)] heapq.heapify(hq) INF=10**15 cost = [INF]*(2*n*n) cost[0] = 0 while hq: c, v = heapq.heappop(hq) if c > cost[v]: continue for u, d in g[v]: tmp=d+cost[v] if tmp < cost[u]: cost[u] = tmp heapq.heappush(hq, (tmp, u)) print(min(cost[n*n-1],cost[2*n*n-1])) calc()