Official

I - ドローン配送と高度制御 / Drone Delivery and Altitude Control Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

We enumerate all possible allowed altitude ranges \([U,V]\), and for each range, find the shortest path on a graph where each state represents a “base and altitude” pair. The minimum value of the altitude range configuration time plus the shortest path travel time is the answer.

Analysis

Base Alone Cannot Represent the State

Even at the same base, the available flight routes and travel times differ depending on the altitude. Therefore, an approach that records only a single shortest time for each base will not work correctly.

Thus, we define the state as:

\((i,h)\): being at base \(i\) at altitude \(h\).

Since the altitude ranges from \(0\) to \(K\), the number of states is \(N(K+1)\).

With this formulation, both changing altitudes and traversing flight routes can be represented as directed edges between states.

We Can Enumerate All Altitude Ranges

To minimize the configuration time, we want the altitude range to be as narrow as possible; however, making it too narrow restricts the flight routes that can be used.

For example, even if \(S=T=2\), it may be optimal to use a route that can only be traversed at altitude \(3\). Therefore, simply setting \([U,V]=[\min(S,T),\max(S,T)]\) is not necessarily sufficient.

On the other hand, since \(K\leq 10\), the number of intervals satisfying \(0\leq U\leq V\leq K\) is at most

\(\dfrac{(K+1)(K+2)}{2}\leq 66\).

Furthermore, since the range must include the initial altitude and the target altitude, it is sufficient to examine only intervals that satisfy:

  • \(0\leq U\leq \min(S,T)\)
  • \(\max(S,T)\leq V\leq K\)

Fixing the Interval Reduces It to a Standard Shortest Path Problem

Attempting to search through all paths directly would lead to an endless number of candidates, as the same bases and flight routes can be visited multiple times.

However, once the interval \([U,V]\) is fixed, it becomes a shortest path problem using only the states within that interval. Since all travel times are non-negative, this can be solved using Dijkstra’s algorithm.

Algorithm

1. Construct the “Base and Altitude” Graph

Treat each state \((i,h)\) as a vertex and add the following directed edges.

Edges for Changing Altitude

For each base \(i\) and \(0\leq h<K\), add:

  • \((i,h)\rightarrow(i,h+1)\) with weight \(P_i\)
  • \((i,h+1)\rightarrow(i,h)\) with weight \(Q_i\)

Since ascending and descending may take different amounts of time, they are added as separate directed edges.

Edges for Traversing Flight Routes

For flight route \(j\) and for every integer altitude \(h\) satisfying \(L_j\leq h\leq R_j\), add an edge:

\((A_j,h)\rightarrow(B_j,h)\)

with weight

\(C_j+D_j|h-H_j|\).

Traversing a route does not change the altitude, so the altitude remains the same before and after the move.

2. Enumerate Allowed Altitude Ranges

Enumerate all intervals \([U,V]\) that contain the initial altitude \(S\) and the target altitude \(T\).

The configuration time for each interval is

\(\mathrm{fee}=G(V-U)\).

In the code, we process the intervals in ascending order of this configuration time.

3. Run Dijkstra’s Algorithm for Each Interval

Set the starting state to \((1,S)\) and the destination state to \((N,T)\).

Without reconstructing the graph, when examining an edge, we only use it if the destination altitude falls within \([U,V]\). Since the start altitude is also within the interval, all explored states are guaranteed to remain within the interval.

If the shortest time to the destination is \(d\), the minimum total time required for this interval is

\(\mathrm{fee}+d\).

We update the answer using this value.

4. Prune the Search Using the Current Best Answer

In the code, using the minimum value obtained so far as answer, the following pruning is performed:

  • Do not inspect intervals whose configuration time alone is already \(\ge\) answer.
    Since travel times are non-negative, such intervals cannot improve the answer. Because intervals are processed in ascending order of configuration time, all subsequent intervals can also be skipped.

  • Do not explore states whose travel time is \(\ge\) answer - fee.
    Even if the destination could be reached from such a state, the total time could not be strictly less than answer.

The latter is implemented by initializing the distance array with answer - fee and only recording distances that are strictly smaller than that value.

Justification of Correctness

For a fixed interval \([U,V]\), the altitude changes and route traversals that are actually permissible correspond directly to the edges of the constructed graph. Furthermore, since transitions to states outside the interval are prohibited, any path on the graph corresponds to a valid itinerary.

Therefore, the shortest distance obtained by Dijkstra’s algorithm is the minimum time required for travel and altitude adjustments within that interval.

Since we enumerate all valid altitude ranges and only prune candidates that cannot improve upon the current best answer, the minimum value after adding the configuration time is indeed the optimal solution to the entire problem.

If the destination cannot be reached under any interval, output NA.

Complexity

The number of vertices is \(N(K+1)\), the number of edges is at most \(2NK+M(K+1)\), and the number of examined intervals is \(O(K^2)\).

In an implementation like the provided code where an element is pushed into the priority queue with each update, the complexities can be evaluated as follows:

  • Time Complexity: \(O\!\left(K^3(N+M)\log\bigl(K(N+M)\bigr)\right)\)
  • Space Complexity: \(O\!\left(K(N+M)+K^2\right)\)

The space complexity includes the storage for the graph, distance array, and priority queue, as well as the list of enumerated intervals. While pruning reduces the actual amount of search, the worst-case complexity remains the same.

Implementation Notes

  • Representing states as integers
    Using 0-based indexing for bases, a state \((i,h)\) can be represented as i * (K + 1) + h. The starting state is S, and the destination state is (N - 1) * (K + 1) + T.

  • Terminating the search when the destination is popped
    In Dijkstra’s algorithm, once the destination is popped from the priority queue (after discarding outdated entries), its shortest distance is finalized.

  • Zero weights are handled correctly
    Dijkstra’s algorithm only requires edge weights to be non-negative. By updating only when the distance strictly decreases, cycles with weight \(0\) can be handled properly.

  • Calculating time directly even if the reference altitude is outside the permissible range
    Whether a route can be used is determined by \(L_j\leq h\leq R_j\), and the time is always computed as \(C_j+D_j|h-H_j|\). There is no need to clamp \(H_j\) to within the range.

  • Using a sufficiently large integer type
    The total required time may not fit in a \(32\)-bit integer. While Python handles arbitrary-precision integers automatically, \(64\)-bit integers should be used in other languages.

    Source Code

import sys
from heapq import heappush, heappop


def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    it = iter(data)

    N = next(it)
    M = next(it)
    K = next(it)
    G = next(it)
    S = next(it)
    T = next(it)

    H = K + 1
    size = N * H
    graph = [[] for _ in range(size)]

    for i in range(N):
        p = next(it)
        q = next(it)
        base = i * H
        for h in range(K):
            u = base + h
            graph[u].append((u + 1, p))
            graph[u + 1].append((u, q))

    for _ in range(M):
        a = next(it) - 1
        b = next(it) - 1
        l = next(it)
        r = next(it)
        c = next(it)
        h0 = next(it)
        d = next(it)

        abase = a * H
        bbase = b * H
        for h in range(l, r + 1):
            graph[abase + h].append(
                (bbase + h, c + d * abs(h - h0))
            )

    del data, it

    heights = list(range(H)) * N
    start = S
    goal = (N - 1) * H + T

    low = min(S, T)
    high = max(S, T)
    intervals = [
        (G * (v - u), u, v)
        for u in range(low + 1)
        for v in range(high, H)
    ]
    intervals.sort()

    INF = 1 << 60
    answer = INF

    for fee, lower, upper in intervals:
        if fee >= answer:
            break

        limit = answer - fee
        dist = [limit] * size
        dist[start] = 0
        heap = [(0, start)]

        while heap:
            cost, u = heappop(heap)
            if cost != dist[u]:
                continue

            if u == goal:
                answer = fee + cost
                break

            for v, weight in graph[u]:
                if lower <= heights[v] <= upper:
                    new_cost = cost + weight
                    if new_cost < dist[v]:
                        dist[v] = new_cost
                        heappush(heap, (new_cost, v))

    print("NA" if answer == INF else answer)


if __name__ == "__main__":
    main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: