公式

E - 配達ロボットの巡回 / Delivery Robot's Patrol 解説 by admin 印刷ページ

GPT 5.2 High

Overview

We visit all storage locations (at most \(15\)) and find the minimum number of steps to return to the starting point \((1,1)\), using Manhattan distance and bit DP (Traveling Salesman Problem-style DP).

Analysis

  • Since we can move up, down, left, and right on the grid, the shortest distance between two points \((r_1,c_1)\) and \((r_2,c_2)\) is
    \(|r_1-r_2| + |c_1-c_2|\) (Manhattan distance), as long as there are no obstacles.
    Since there are no obstacles in this problem, there is no need to perform pathfinding (such as BFS) each time.
  • What we want is “the shortest path starting from \((1,1)\), visiting all storage locations at least once, and returning to \((1,1)\).”
    This is a problem of optimizing the “visit order.” Naively trying all permutations gives \(K!\) possibilities, and even for \(K=15\), \(15! \approx 1.3\times 10^{12}\), which is far too many.
  • However, since \(K \le 15\) is small, we can use a DP that manages “which storage locations have been visited” as a bitmask (the classic TSP DP).
    This reduces the complexity to \(O(2^K K^2)\).

Algorithm

  1. Preprocessing (Distance Computation)

    • Distance from the start \((1,1)\) to each storage location \(i\):
      \(dist0[i] = (R_i-1) + (C_i-1)\)
    • Distance matrix between storage locations:
      \(dist[i][j] = |R_i-R_j| + |C_i-C_j|\)
  2. Bit DP (Traveling Salesman Problem-style)

    • State:
      • \(mask\): the set of visited storage locations (\(K\) bits)
      • \(i\): the index of the last storage location visited
    • DP definition:
      \(dp[mask][i] =\) “the minimum number of steps starting from \((1,1)\), having visited the set \(mask\), and currently at location \(i\)”
    • Initialization: when visiting \(i\) first
      • \(dp[1<<i][i] = dist0[i]\)
    • Transition: move to an unvisited location \(j\)
      • \(dp[mask \cup (1<<j)][j] = \min(dp[mask \cup (1<<j)][j],\ dp[mask][i] + dist[i][j])\)
    • Answer: after visiting all locations, return from the last location \(i\) to \((1,1)\)
      • \(ans = \min_i \{ dp[(1<<K)-1][i] + dist0[i] \}\)
  3. Special Case

    • If \(K=0\) (no storage locations), no movement is needed, so the answer is \(0\).

(Example) When \(K=3\), \(mask=101_2\) represents “locations 0 and 2 have been visited,” and from there we transition to unvisited location 1 to create \(mask=111_2\).

Complexity

  • Time complexity:
    Distance precomputation is \(O(K^2)\), and the DP is \(O(2^K K^2)\), so the overall complexity is \(O(2^K K^2)\).
  • Space complexity:
    The DP table is \(O(2^K K)\), and the distance matrix is \(O(K^2)\), so the overall space is \(O(2^K K)\).

Implementation Notes

  • For \(K=15\), \(2^K=32768\), so storing \(dp\) as a two-dimensional array initialized with INF is perfectly feasible.

  • Since all distances are Manhattan distances, even if the grid size \(N,M\) is as large as \(10^5\), there is no issue (no pathfinding needed).

  • Don’t forget to add the “return trip (back to \((1,1)\))” at the end (dp[full][i] + dist0[i]).

    Source Code

import sys

def main():
    input = sys.stdin.readline
    N, M, K = map(int, input().split())
    if K == 0:
        print(0)
        return

    pts = [tuple(map(int, input().split())) for _ in range(K)]
    rs = [p[0] for p in pts]
    cs = [p[1] for p in pts]

    dist0 = [(rs[i] - 1) + (cs[i] - 1) for i in range(K)]
    dist = [[0] * K for _ in range(K)]
    for i in range(K):
        ri, ci = rs[i], cs[i]
        for j in range(i + 1, K):
            d = abs(ri - rs[j]) + abs(ci - cs[j])
            dist[i][j] = d
            dist[j][i] = d

    INF = 10**30
    size = 1 << K
    dp = [[INF] * K for _ in range(size)]
    for i in range(K):
        dp[1 << i][i] = dist0[i]

    for mask in range(size):
        cur = dp[mask]
        for i in range(K):
            base = cur[i]
            if base == INF:
                continue
            for j in range(K):
                bit = 1 << j
                if mask & bit:
                    continue
                nm = mask | bit
                v = base + dist[i][j]
                if v < dp[nm][j]:
                    dp[nm][j] = v

    full = size - 1
    ans = INF
    for i in range(K):
        v = dp[full][i] + dist0[i]
        if v < ans:
            ans = v
    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

投稿日時:
最終更新: