公式

E - 照明スイッチ / Light Switch 解説 by admin 印刷ページ

GLM 5.2 (High)

Overview

By treating the ON/OFF states of the lights as nodes of a graph and the switch operations as edges, we can solve this problem as a shortest path problem. The constraint that we cannot pass through danger patterns can be handled as the removal of edges, and we can find the minimum cost using Dijkstra’s algorithm.

Analysis

First, let’s organize the elements of the problem. - There are \(N\) lights, and each light is either “ON” or “OFF”. Therefore, there are \(2^N\) possible combinations of light states. Given the constraint \(N \leq 18\), the total number of states is at most \(2^{18} = 262,144\). This is small enough that we can explore all states. - Operating switch panel \(j\) toggles the state of light \(i\) for which \(b_{j,i} = 1\). This means the next state can be computed by taking the bitwise exclusive OR (XOR) of the current state’s bits and the switch’s action pattern’s bits. - However, the state immediately after pressing a switch must not match any “danger pattern”. In other words, if the destination state is a danger pattern, we cannot choose the operation (edge) of pressing that switch.

From these observations, this problem can be reduced to the following shortest path problem: - Nodes: The state of the lights (represented as integers from \(0\) to \(2^N-1\)) - Edges: Transitioning from node \(u\) to node \(v\) (\(v = u \oplus b_j\)) by operating switch \(j\). An edge exists only if \(v\) is not a danger pattern. The weight of the edge is the cost of the switch \(C_j\). - Goal: Find the minimum cost path from the initial state (all \(0\)) to the target state (all \(1\)).

Since all costs are positive values (\(1 \leq C_j \leq 10^6\)), we can apply Dijkstra’s algorithm as this is a shortest path problem with no negative edges.

Algorithm

We search for the shortest path from state 0 to state 2^N - 1 using Dijkstra’s algorithm.

  1. State Representation: Represent the state of the lights using a bitmask. For example, if lights 1 and 3 are ON, the state is represented as 101 (binary) = 5 (decimal). The target state is the state where all bits are set, i.e., (1 << N) - 1.
  2. Recording Danger Patterns: Prepare an array is_danger of length \(2^N\), and set the indices corresponding to the given \(K\) danger patterns to true (or 1).
  3. Executing Dijkstra’s Algorithm:
    • Initialize a distance array dist with infinity, and set dist[0] = 0.
    • Push (cost, state) = (0, 0) into a min-heap (priority queue).
    • Pop the state u with the minimum cost from the queue.
    • If u is the target state (1 << N) - 1, terminate the search; the current cost is the answer.
    • For all switches \(j\), compute the next state v = u ^ b[j].
    • If v is not a danger pattern (is_danger[v] is false) and dist[u] + C[j] is smaller than dist[v], update dist[v] and push (dist[v], v) into the queue.
  4. After the search, if the distance of the target state dist[(1 << N) - 1] remains infinity, it is unreachable (output -1); otherwise, output that value.

Complexity

  • Time Complexity: \(O(M \cdot 2^N \log(2^N)) = O(M \cdot 2^N \cdot N)\)
    • Since the number of nodes is \(V = 2^N\) and we check \(M\) edges from each node, the number of edges is \(E = M \cdot 2^N\). The time complexity of Dijkstra’s algorithm is \(O(E \log V)\). With \(N \leq 18\) and \(M \leq 20\), the number of operations is approximately \(20 \times 2^{18} \times 18 \approx 9.4 \times 10^7\), which easily runs within the time limit.
  • Space Complexity: \(O(2^N)\)
    • This is because we use arrays whose sizes are proportional to the number of states \(2^N\), such as the distance array and the danger pattern array.

Implementation Points

  • By managing the light states and the switch action patterns as integers (bitmasks), state transitions can be written concisely and quickly in a single line using the XOR operation u ^ b[i].

  • Checking for danger patterns can be done in \(O(1)\) time by using an array. Using a set (hash table) may result in a heavy constant factor, so an array is recommended.

  • Since the sum of the shortest distances can be large, use a sufficiently large integer type such as long long for the dist array.

    Source Code

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

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

    vector<int> b(M);
    vector<long long> C(M);
    for (int i = 0; i < M; ++i) {
        int mask = 0;
        for (int j = 0; j < N; ++j) {
            int bit;
            cin >> bit;
            if (bit) mask |= (1 << j);
        }
        cin >> C[i];
        b[i] = mask;
    }

    int total_states = 1 << N;
    vector<char> is_danger(total_states, 0);
    for (int i = 0; i < K; ++i) {
        int mask = 0;
        for (int j = 0; j < N; ++j) {
            int bit;
            cin >> bit;
            if (bit) mask |= (1 << j);
        }
        is_danger[mask] = 1;
    }

    const long long INF = 1e18;
    vector<long long> dist(total_states, INF);
    
    // (cost, state)
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;

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

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

        if (d > dist[u]) continue;

        if (u == total_states - 1) {
            break;
        }

        for (int i = 0; i < M; ++i) {
            int v = u ^ b[i];
            if (!is_danger[v]) {
                if (dist[u] + C[i] < dist[v]) {
                    dist[v] = dist[u] + C[i];
                    pq.push({dist[v], v});
                }
            }
        }
    }

    if (dist[total_states - 1] == INF) {
        cout << -1 << "\n";
    } else {
        cout << dist[total_states - 1] << "\n";
    }

    return 0;
}

This editorial was generated by or-glm5.2-high.

投稿日時:
最終更新: