結果

問題 No.1283 Extra Fee
ユーザー ygd.ygd.
提出日時 2020-11-07 23:40:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 944 ms / 2,000 ms
コード長 1,658 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 169,336 KB
最終ジャッジ日時 2024-04-27 23:37:49
合計ジャッジ時間 14,448 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,992 KB
testcase_01 AC 38 ms
52,480 KB
testcase_02 AC 43 ms
53,120 KB
testcase_03 AC 38 ms
52,480 KB
testcase_04 AC 35 ms
52,964 KB
testcase_05 AC 36 ms
52,608 KB
testcase_06 AC 38 ms
53,632 KB
testcase_07 AC 35 ms
52,992 KB
testcase_08 AC 39 ms
54,400 KB
testcase_09 AC 37 ms
53,632 KB
testcase_10 AC 36 ms
53,760 KB
testcase_11 AC 210 ms
82,040 KB
testcase_12 AC 187 ms
81,688 KB
testcase_13 AC 154 ms
80,384 KB
testcase_14 AC 256 ms
90,480 KB
testcase_15 AC 308 ms
97,484 KB
testcase_16 AC 161 ms
82,136 KB
testcase_17 AC 839 ms
139,936 KB
testcase_18 AC 897 ms
160,064 KB
testcase_19 AC 892 ms
167,096 KB
testcase_20 AC 884 ms
159,264 KB
testcase_21 AC 849 ms
159,896 KB
testcase_22 AC 772 ms
149,532 KB
testcase_23 AC 728 ms
155,976 KB
testcase_24 AC 812 ms
164,164 KB
testcase_25 AC 894 ms
168,864 KB
testcase_26 AC 912 ms
168,668 KB
testcase_27 AC 944 ms
168,688 KB
testcase_28 AC 937 ms
169,336 KB
testcase_29 AC 894 ms
145,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 for _ in range(2*V)] #d[i][j];j=0権利を行使していないj=1どこかで権利を行使済み
  #行使済みの点は i + V (V~2*V-1)、未行使の点はi (0~V-1)
  d[s] = 0; d[s+V] = 0
  PQ = []
  heappush(PQ,(0,s)) #コスト0,初期点s,権利行使していない
  
  while PQ:
    c,v = heappop(PQ)
    if d[v] < c:
      continue
    d[v] = c
    if v >= V:
      oriv = v - V
    else:
      oriv = v
    for cost,u in G[oriv]:
      if v < V: #未行使
        if d[u] <= cost + d[v]:
          pass
        else:
          d[u] = cost + d[v]
          heappush(PQ,(d[u], u))
        if d[u+V] <= 1 + d[v]: #移動の1は必ずかかる。
          pass
        else:
          d[u+V] = 1 + d[v]
          heappush(PQ,(d[u+V], u+V))
      else: #行使済み
        if d[u+V] <= cost + d[v]:
          pass
        else:
          d[u+V] = cost + d[v]
          heappush(PQ,(d[u+V], u+V))
  
  return d

D = dijkstra_heap2(0,G)
#print(D)
ans = min(D[N*N-1],D[2*N*N-1])

print(ans)
0