公式

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

DeepSeek V3

Overview

This problem is a variant of the Traveling Salesman Problem (TSP) where we need to visit all storage locations on a grid at least once and return to the starting point, finding the shortest path to do so.

Analysis

Since the grid size can be up to \(10^5 \times 10^5\), which is very large, searching over all cells is impossible. However, a key observation is that the number of storage locations \(K\) is at most 15, which is very small.

In this problem, only the starting point (cell \((1,1)\)) and each storage location are important points. The distances between these points can be calculated using Manhattan distance (\(|r_1 - r_2| + |c_1 - c_2|\)).

As a straightforward approach, we can precompute the shortest paths between all points and solve the Traveling Salesman Problem using only these points as vertices. By solving TSP on \(K+1\) vertices (including the starting point), we can efficiently find the answer.

Algorithm

  1. Preprocessing: Convert the coordinates of the input storage locations to 0-indexed and create a point list that includes the starting point \((0,0)\)
  2. Distance matrix computation: Calculate the Manhattan distance between each pair of points and create an \(n \times n\) distance matrix (\(n = K + 1\))
  3. Solving TSP: Solve the Traveling Salesman Problem using bitmask DP
    • dp[mask][i]: The minimum number of steps when the set of visited points is mask and the last visited point is \(i\)
    • Initial state: Only the starting point is visited (mask=1), with cost 0
    • Transition: For each unvisited point \(j\), update by adding the travel cost from \(i\) to \(j\)
  4. Computing the answer: After visiting all points, add the cost of returning from the last point to the starting point, and find the minimum value

Complexity

  • Time complexity: \(O(n^2 \cdot 2^n)\) (\(n = K + 1 \leq 16\))
  • Space complexity: \(O(n \cdot 2^n)\)

Implementation Notes

  • Don’t forget to handle the case where the number of storage locations is 0

  • The distance matrix is symmetric, so you could save computation by only calculating the lower half, but for simplicity of implementation, we compute all pairs

  • In the bitmask DP implementation, be careful with INF value initialization and overflow (initialize with \(10^{18}\))

  • The number of states is \(2^{16} \times 16 = 1048576\), which is well within the constraints

    Source Code

import sys

def main():
    data = sys.stdin.read().split()
    if not data:
        print(0)
        return
        
    it = iter(data)
    N = int(next(it)); M = int(next(it)); K = int(next(it))
    points = []
    for i in range(K):
        r = int(next(it)); c = int(next(it))
        points.append((r-1, c-1))
        
    if K == 0:
        print(0)
        return
        
    start = (0, 0)
    points_with_start = [start] + points
    n_nodes = len(points_with_start)
    
    dists = [[0] * n_nodes for _ in range(n_nodes)]
    for i in range(n_nodes):
        r1, c1 = points_with_start[i]
        for j in range(i+1, n_nodes):
            r2, c2 = points_with_start[j]
            d = abs(r1 - r2) + abs(c1 - c2)
            dists[i][j] = d
            dists[j][i] = d
            
    INF = 10**18
    dp = [[INF] * n_nodes for _ in range(1<<n_nodes)]
    dp[1][0] = 0
    
    for mask in range(1<<n_nodes):
        for i in range(n_nodes):
            if dp[mask][i] == INF:
                continue
            for j in range(n_nodes):
                if mask & (1<<j):
                    continue
                new_mask = mask | (1<<j)
                new_cost = dp[mask][i] + dists[i][j]
                if new_cost < dp[new_mask][j]:
                    dp[new_mask][j] = new_cost
                    
    ans = INF
    full_mask = (1<<n_nodes) - 1
    for i in range(n_nodes):
        total = dp[full_mask][i] + dists[i][0]
        if total < ans:
            ans = total
            
    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by deepseekv3.

投稿日時:
最終更新: