結果

問題 No.1283 Extra Fee
ユーザー tamatotamato
提出日時 2020-11-06 21:51:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 846 ms / 2,000 ms
コード長 1,608 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 143,568 KB
最終ジャッジ日時 2024-04-27 23:14:50
合計ジャッジ時間 10,759 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,700 KB
testcase_01 AC 34 ms
53,304 KB
testcase_02 AC 34 ms
54,004 KB
testcase_03 AC 34 ms
54,628 KB
testcase_04 AC 34 ms
53,320 KB
testcase_05 AC 34 ms
53,296 KB
testcase_06 AC 38 ms
54,780 KB
testcase_07 AC 34 ms
54,552 KB
testcase_08 AC 36 ms
54,212 KB
testcase_09 AC 37 ms
53,988 KB
testcase_10 AC 36 ms
54,480 KB
testcase_11 AC 138 ms
80,176 KB
testcase_12 AC 116 ms
80,452 KB
testcase_13 AC 101 ms
78,764 KB
testcase_14 AC 180 ms
87,268 KB
testcase_15 AC 218 ms
93,172 KB
testcase_16 AC 114 ms
80,260 KB
testcase_17 AC 699 ms
138,448 KB
testcase_18 AC 557 ms
130,624 KB
testcase_19 AC 592 ms
132,964 KB
testcase_20 AC 543 ms
129,500 KB
testcase_21 AC 542 ms
130,324 KB
testcase_22 AC 512 ms
124,388 KB
testcase_23 AC 522 ms
134,328 KB
testcase_24 AC 545 ms
134,776 KB
testcase_25 AC 567 ms
134,804 KB
testcase_26 AC 589 ms
135,140 KB
testcase_27 AC 576 ms
135,080 KB
testcase_28 AC 846 ms
135,332 KB
testcase_29 AC 770 ms
143,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.buffer.readline
    import heapq

    def dijkstra(adj, start):
        # adj: [[to, cost] * vertices], 0th index must be empty
        inf = 1 << 60
        dist = [inf] * len(adj)
        dist[start] = 0
        q = []
        heapq.heappush(q, (0, start))
        while q:
            min_dist, v_from = heapq.heappop(q)
            if min_dist > dist[v_from]:
                continue
            v_tos = adj[v_from]
            for v_to, cost in v_tos:
                if min_dist + cost < dist[v_to]:
                    dist[v_to] = min_dist + cost
                    heapq.heappush(q, (dist[v_to], v_to))
        return dist

    N, M = map(int, input().split())
    grid = [[0] * (N+1) for _ in range(N+1)]
    for _ in range(M):
        h, w, c = map(int, input().split())
        grid[h][w] = c

    d = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    adj = [[] for _ in range(N*N + 1)]
    for h in range(1, N+1):
        for w in range(1, N+1):
            hw = (h-1) * N + w
            for dh, dw in d:
                h_new, w_new = h+dh, w+dw
                if 1 <= h_new <= N and 1 <= w_new <= N:
                    hw_new = (h_new - 1) * N + w_new
                    adj[hw].append((hw_new, grid[h_new][w_new] + 1))

    dist1 = dijkstra(adj, 1)
    dist2 = dijkstra(adj, N*N)
    ans = float("inf")
    for h in range(1, N+1):
        for w in range(1, N+1):
            hw = (h-1) * N + w
            ans = min(ans, dist1[hw] + dist2[hw] - grid[h][w] * 2)
    print(ans)


if __name__ == '__main__':
    main()
0