結果

問題 No.1283 Extra Fee
ユーザー 👑 tamatotamato
提出日時 2020-11-06 21:51:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 890 ms / 2,000 ms
コード長 1,608 bytes
コンパイル時間 585 ms
コンパイル使用メモリ 87,632 KB
実行使用メモリ 145,636 KB
最終ジャッジ日時 2023-08-10 05:56:47
合計ジャッジ時間 13,362 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,528 KB
testcase_01 AC 71 ms
71,808 KB
testcase_02 AC 75 ms
71,596 KB
testcase_03 AC 74 ms
71,624 KB
testcase_04 AC 73 ms
71,768 KB
testcase_05 AC 73 ms
71,492 KB
testcase_06 AC 75 ms
71,828 KB
testcase_07 AC 75 ms
71,804 KB
testcase_08 AC 76 ms
71,916 KB
testcase_09 AC 74 ms
71,692 KB
testcase_10 AC 73 ms
71,640 KB
testcase_11 AC 185 ms
81,360 KB
testcase_12 AC 164 ms
81,524 KB
testcase_13 AC 146 ms
80,548 KB
testcase_14 AC 228 ms
88,892 KB
testcase_15 AC 276 ms
94,352 KB
testcase_16 AC 159 ms
81,592 KB
testcase_17 AC 837 ms
139,700 KB
testcase_18 AC 654 ms
132,632 KB
testcase_19 AC 680 ms
134,968 KB
testcase_20 AC 653 ms
131,128 KB
testcase_21 AC 649 ms
132,496 KB
testcase_22 AC 596 ms
125,924 KB
testcase_23 AC 634 ms
136,212 KB
testcase_24 AC 651 ms
135,936 KB
testcase_25 AC 689 ms
136,616 KB
testcase_26 AC 706 ms
136,912 KB
testcase_27 AC 699 ms
136,488 KB
testcase_28 AC 728 ms
136,692 KB
testcase_29 AC 890 ms
145,636 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