E - 照明スイッチ / Light Switch Editorial by admin Editorial for printing
gpt-5.5-highSummary
We represent the state of the lights as a bit string and treat the switch operations as “bit inversions” (XOR). This is a shortest path problem to find the minimum cost to go from the initial state \(0\) to the all-on state \(2^N-1\) while avoiding dangerous patterns.
Analysis
Since each light has \(2\) states, “off \(0\) / on \(1\)”, there are \(2^N\) possible states for the entire room.
Since \(N \leq 18\), we have
\[ 2^N \leq 2^{18} = 262144 \]
which allows us to manage all states.
We represent each state as an integer bitmask.
For example, when \(N=3\), the state 101 represents:
- Light \(1\): On
- Light \(2\): Off
- Light \(3\): On
The switch panels can also be represented as bit strings. If we represent the lights affected by a switch as \(1\) and the unaffected ones as \(0\), operating that switch corresponds to taking the XOR with the current state.
If the current state is \(v\) and the bit string of the switch is \(mask\), the state after the operation is:
\[ v \oplus mask \]
For example, if the current state is 101 and the switch affects 011:
\[ 101 \oplus 011 = 110 \]
However, the state after the operation must not be a dangerous pattern. Therefore, we can treat dangerous patterns as “unvisitable vertices”.
This problem can be formulated as the shortest path problem on the following graph:
- Vertices: Each light state \(0, 1, \ldots, 2^N-1\)
- Edges: Each switch operation
- Transition from state \(v\) to \(v \oplus mask\)
- Cost is the cost of the switch
- Cannot move to vertices representing dangerous patterns
Since the switch costs are not necessarily all the same, we cannot find the minimum cost using a simple BFS. Because it is a shortest path problem with weighted edges, we use Dijkstra’s algorithm.
Algorithm
- Convert each switch panel into a bitmask.
- If there are multiple switches with the same bitmask, keep only the one with the minimum cost.
- If they cause the same state transition, it is sufficient to use only the one with the smaller cost.
- Convert the dangerous patterns into bitmasks and record them as
forbidden[mask] = true. - Run Dijkstra’s algorithm with
dist[state]representing the “minimum cost from the initial state tostate”. - From state \(v\), try each switch and calculate the next state:
$\( nv = v \oplus mask \)$
- If
nvis a dangerous pattern, do not transition to it. - Otherwise, update the distance.
- If the all-on state \(goal = 2^N - 1\) is reachable, output its cost; otherwise, output
-1.
Complexity
Let \(U\) be the number of distinct non-zero bitmasks among the switches. Note that \(U \leq M\).
- Time Complexity: \(O(2^N \cdot U \log 2^N)\)
- Space Complexity: \(O(2^N + U)\)
Given the constraints \(N \leq 18\) and \(M \leq 20\), this will easily run within the time limit.
Key Implementation Details
The number of states can be represented as
1 << N.The all-on state is
goal = (1 << N) - 1.Switch operations can be written using XOR as
nv = v ^ mask.Since the total cost can be large, use
long longfor the distance array.Switches with
mask == 0do not change the state and only incur a positive cost, so there is no point in using them. They can be ignored.If there are multiple switches with the exact same
mask, keeping only the one with the minimum cost can reduce unnecessary transitions.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M, K;
cin >> N >> M >> K;
vector<pair<int, long long>> ops;
map<int, long long> best_cost;
for (int j = 0; j < M; j++) {
int mask = 0;
for (int i = 0; i < N; i++) {
int b;
cin >> b;
if (b) mask |= (1 << i);
}
long long c;
cin >> c;
if (mask == 0) continue;
if (!best_cost.count(mask) || best_cost[mask] > c) {
best_cost[mask] = c;
}
}
for (auto [mask, cost] : best_cost) {
ops.push_back({mask, cost});
}
int S = 1 << N;
int goal = S - 1;
vector<char> forbidden(S, 0);
for (int k = 0; k < K; k++) {
int mask = 0;
for (int i = 0; i < N; i++) {
int f;
cin >> f;
if (f) mask |= (1 << i);
}
forbidden[mask] = 1;
}
const long long INF = (1LL << 62);
vector<long long> dist(S, INF);
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, v] = pq.top();
pq.pop();
if (d != dist[v]) continue;
if (v == goal) break;
for (auto [mask, cost] : ops) {
int nv = v ^ mask;
if (forbidden[nv]) continue;
long long nd = d + cost;
if (nd < dist[nv]) {
dist[nv] = nd;
pq.push({nd, nv});
}
}
}
if (dist[goal] == INF) {
cout << -1 << '\n';
} else {
cout << dist[goal] << '\n';
}
return 0;
}
This editorial was generated by gpt-5.5-high.
posted:
last update: