Official

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

Gemini 3.8 Flash (High)

Overview

This problem asks for the minimum time required to travel from base \(1\) (altitude \(S\)) to base \(N\) (altitude \(T\)) by appropriately setting the flight altitude bounds \([U, V]\) for the drone. Noticing that the maximum altitude \(K\) is very small (\(K \leq 10\)), we can find the optimal solution by iterating over all possible altitude ranges \([U, V]\) and solving a single-source shortest path problem (Dijkstra’s algorithm) on an extended graph for each range.

Analysis

1. Focusing on the Constraints

The most distinctive constraint in this problem is \(K \le 10\). There are only \(11\) possible choices for altitude, from \(0\) to \(10\).

The altitude range \([U, V]\) (\(0 \leq U \leq V \leq K\)) that the drone must adhere to must contain both the initial altitude \(S\) and the destination altitude \(T\), so it must satisfy: $\(0 \leq U \leq \min(S, T) \leq \max(S, T) \leq V \leq K\)\( There are at most \)(K+1)(K+2)/2 \approx 66\( such pairs of \)(U, V)$.

Therefore, an approach of iterating over all possible allowable altitude ranges \([U, V]\) is effective.

2. The Subproblem for a Fixed \([U, V]\)

When the altitude range \([U, V]\) is fixed, the equipment adjustment time \(G \times (V - U)\) becomes a constant. The remaining problem is to find the minimum time to travel from state \((1, S)\) to state \((N, T)\) under the restriction that the altitude must always stay between \(U\) and \(V\), inclusive.

The state of the drone can be represented as the pair \((\text{current base } u, \text{current altitude } x)\). Since \(U \leq x \leq V\), the number of states (vertices) is at most \(N \times (K + 1) \leq 1000 \times 11 = 11000\).

The transitions in this state space are as follows:

  1. Increase altitude by \(1\) at base \(u\):
    • Condition: \(x + 1 \leq V\)
    • Next state: \((u, x + 1)\)
    • Cost: \(P_u\)
  2. Decrease altitude by \(1\) at base \(u\):
    • Condition: \(x - 1 \geq U\)
    • Next state: \((u, x - 1)\)
    • Cost: \(Q_u\)
  3. Use flight route \(j\) (\(A_j \to B_j\)):
    • Condition: \(u = A_j\) and \(L_j \leq x \leq R_j\)
    • Next state: \((B_j, x)\) (altitude does not change)
    • Cost: \(C_j + D_j \times |x - H_j|\)

Since all transition costs are non-negative (at least \(0\)), we can apply Dijkstra’s algorithm to find the single-source shortest paths on this graph.

Algorithm

  1. Initialize a variable ans to store the minimum answer with a sufficiently large value (\(\infty\)).
  2. For all pairs \((U, V)\) satisfying \(0 \leq U \leq \min(S, T)\) and \(\max(S, T) \leq V \leq K\), repeat the following:
    1. Initialize the shortest distance table dist[u][x] for all states \((u, x)\) (\(1 \leq u \leq N, U \leq x \leq V\)) with \(\infty\).
    2. Set the starting state dist[1][S] = 0 and run Dijkstra’s algorithm using a priority queue.
    3. If the state popped from the priority queue is the destination \((N, T)\), we can terminate the search early at that point.
    4. If the destination state \((N, T)\) is reachable, compute the sum of the travel cost dist[N][T] and the altitude range adjustment cost \(G \times (V - U)\), and update ans with this value.
  3. After trying all \((U, V)\), if ans was updated, output its value; if the destination was never reached, output NA.

Complexity

  • Time Complexity: \(O(K^2 \cdot (N K + M K) \log(NK))\)
    • There are \(O(K^2)\) candidates for \((U, V)\).
    • In each run of Dijkstra’s algorithm:
      • Number of vertices \(V' = O(NK)\)
      • Number of edges \(E' = O(NK + MK)\)
      • The complexity is \(O(E' \log V')\).
    • Since \(K \leq 10, N \leq 1000, M \leq 5000\), the number of vertices is at most about \(1.1 \times 10^4\) and the number of edges is at most about \(7.5 \times 10^4\). There are at most \(66\) combinations of \((U, V)\), so the total number of operations is within a few tens of millions, easily running well within the time limit.
  • Space Complexity: \(O(N K + M)\)
    • Requires \(O(N + M)\) memory for the graph representation of the flight routes, and \(O(NK)\) memory for the shortest distance array dist.

Implementation Notes

  • Handling Integer Overflow: Since the total cost may exceed the maximum value of a \(32\)-bit integer (approximately \(2 \times 10^9\)), use 64-bit signed integers (such as long long in C++) for storing distances, calculating costs, and the infinity constant (INF).

  • Early Termination: In Dijkstra’s algorithm, as soon as (u, x) == (N, T) is popped from the priority queue, the shortest distance to \((N, T)\) will never be updated further. Thus, breaking out of the loop (break) at that point can reduce the runtime.

    Source Code

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>

using namespace std;

struct Edge {
    int to;
    int L, R;
    long long C, H, D;
};

const long long INF = 4e18;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M, K;
    long long G;
    if (!(cin >> N >> M >> K >> G)) return 0;

    int S, T;
    cin >> S >> T;

    vector<long long> P(N + 1), Q(N + 1);
    for (int i = 1; i <= N; ++i) {
        cin >> P[i] >> Q[i];
    }

    vector<vector<Edge>> adj(N + 1);
    for (int i = 0; i < M; ++i) {
        int u, v, L, R;
        long long C, H, D;
        cin >> u >> v >> L >> R >> C >> H >> D;
        adj[u].push_back({v, L, R, C, H, D});
    }

    long long ans = INF;

    int min_st = min(S, T);
    int max_st = max(S, T);

    vector<vector<long long>> dist(N + 1, vector<long long>(K + 1, INF));

    for (int U = 0; U <= min_st; ++U) {
        for (int V = max_st; V <= K; ++V) {
            for (int i = 1; i <= N; ++i) {
                for (int x = U; x <= V; ++x) {
                    dist[i][x] = INF;
                }
            }

            using State = pair<long long, pair<int, int>>;
            priority_queue<State, vector<State>, greater<State>> pq;

            dist[1][S] = 0;
            pq.push({0, {1, S}});

            while (!pq.empty()) {
                auto [d, ux] = pq.top();
                pq.pop();
                auto [u, x] = ux;

                if (d > dist[u][x]) continue;
                if (u == N && x == T) {
                    break;
                }

                // 高度を 1 上げる
                if (x + 1 <= V) {
                    if (dist[u][x + 1] > d + P[u]) {
                        dist[u][x + 1] = d + P[u];
                        pq.push({dist[u][x + 1], {u, x + 1}});
                    }
                }

                // 高度を 1 下げる
                if (x - 1 >= U) {
                    if (dist[u][x - 1] > d + Q[u]) {
                        dist[u][x - 1] = d + Q[u];
                        pq.push({dist[u][x - 1], {u, x - 1}});
                    }
                }

                // 航路の利用
                for (const auto& edge : adj[u]) {
                    if (edge.L <= x && x <= edge.R) {
                        long long cost = edge.C + edge.D * std::abs((long long)x - edge.H);
                        if (dist[edge.to][x] > d + cost) {
                            dist[edge.to][x] = d + cost;
                            pq.push({dist[edge.to][x], {edge.to, x}});
                        }
                    }
                }
            }

            if (dist[N][T] < INF) {
                long long total_cost = dist[N][T] + G * (V - U);
                ans = min(ans, total_cost);
            }
        }
    }

    if (ans >= INF) {
        cout << "NA\n";
    } else {
        cout << ans << "\n";
    }

    return 0;
}

This editorial was generated by gemini-3.8-flash-high.

posted:
last update: