結果
問題 | No.1283 Extra Fee |
ユーザー | Coki628 |
提出日時 | 2020-12-08 18:33:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,592 ms / 2,000 ms |
コード長 | 1,972 bytes |
コンパイル時間 | 118 ms |
コンパイル使用メモリ | 82,600 KB |
実行使用メモリ | 110,336 KB |
最終ジャッジ日時 | 2024-04-27 23:45:05 |
合計ジャッジ時間 | 17,343 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
53,248 KB |
testcase_01 | AC | 36 ms
53,632 KB |
testcase_02 | AC | 37 ms
53,376 KB |
testcase_03 | AC | 37 ms
53,248 KB |
testcase_04 | AC | 42 ms
53,248 KB |
testcase_05 | AC | 36 ms
53,360 KB |
testcase_06 | AC | 37 ms
54,016 KB |
testcase_07 | AC | 35 ms
53,248 KB |
testcase_08 | AC | 37 ms
53,888 KB |
testcase_09 | AC | 38 ms
53,888 KB |
testcase_10 | AC | 38 ms
53,376 KB |
testcase_11 | AC | 192 ms
79,224 KB |
testcase_12 | AC | 199 ms
79,360 KB |
testcase_13 | AC | 142 ms
78,080 KB |
testcase_14 | AC | 268 ms
80,244 KB |
testcase_15 | AC | 358 ms
82,560 KB |
testcase_16 | AC | 159 ms
78,464 KB |
testcase_17 | AC | 1,482 ms
108,288 KB |
testcase_18 | AC | 1,049 ms
98,176 KB |
testcase_19 | AC | 1,034 ms
97,280 KB |
testcase_20 | AC | 1,039 ms
96,128 KB |
testcase_21 | AC | 956 ms
96,712 KB |
testcase_22 | AC | 894 ms
93,964 KB |
testcase_23 | AC | 938 ms
93,440 KB |
testcase_24 | AC | 896 ms
93,568 KB |
testcase_25 | AC | 1,121 ms
97,920 KB |
testcase_26 | AC | 1,086 ms
98,176 KB |
testcase_27 | AC | 1,057 ms
98,176 KB |
testcase_28 | AC | 1,085 ms
98,688 KB |
testcase_29 | AC | 1,592 ms
110,336 KB |
ソースコード
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10**9) INF = 10**19 MOD = 10**9 + 7 EPS = 10**-10 def dijkstra(grid: list, src: tuple) -> list: """ グリッドダイクストラ(縦, 横, H*Wグリッド, 始点(h, w)) """ from heapq import heappush, heappop H, W = len(grid), len(grid[0]) res = list3d(2, H, W, INF) srch, srcw = src que = [(0, 0, srch, srcw)] res[0][srch][srcw] = 0 directions = ((1, 0), (-1, 0), (0, 1), (0, -1)) while que: dist, used, h, w = heappop(que) if res[used][h][w] < dist: continue for dh, dw in directions: nh, nw = h + dh, w + dw if grid[nh][nw] == -1: continue cost = grid[nh][nw] if dist + cost < res[used][nh][nw]: res[used][nh][nw] = dist + cost heappush(que, (dist+cost, used, nh, nw)) if cost > 1 and not used: cost = 1 if dist + cost < res[1][nh][nw]: res[1][nh][nw] = dist + cost heappush(que, (dist+cost, 1, nh, nw)) return res N, M = MAP() grid = list2d(N+2, N+2, 1) for i in range(N+2): grid[i][0] = -1 grid[i][N+1] = -1 grid[0][i] = -1 grid[N+1][i] = -1 for i in range(M): h, w, c = MAP() grid[h][w] += c res = dijkstra(grid, (1, 1)) ans = min(res[0][N][N], res[1][N][N]) print(ans)