E - 照明スイッチ / Light Switch 解説 by admin 印刷ページ
claude4.8opus-highOverview
By representing the on/off states of the \(N\) lights as a bit string (state) and considering the cost of state transitions via the switch panels, this problem asks for the minimum cost to reach the target state (all lights on) from the initial state (all lights off) without passing through any dangerous patterns. This can be reduced to the shortest path problem on a weighted graph.
Analysis
First, the on/off state of each light can be represented as an \(N\)-bit integer. Since \(N \leq 18\), there are at most \(2^{18} = 262144\) possible states. This is the key to solving this problem.
Representing Switch Panel Operations with Bitwise Operations
Switch panel \(j\) performs the operation of “toggling the lights where \(b_{j,i}=1\)”. Using a bitmask \(\text{mask}_j\) to represent the set of lights affected by switch panel \(j\), the next state \(v\) from the current state \(u\) can be obtained via the XOR operation:
\[v = u \oplus \text{mask}_j\]
(since toggling can be represented by XOR). The cost of this transition is \(C_j\).
Graph Formulation
We can model this as the following graph:
- Vertices: States of the lights (integers from \(0\) to \(2^N - 1\)).
- Edges: Directed edges from each state \(u\) to \(u \oplus \text{mask}_j\) with cost \(C_j\) for each switch panel \(j\).
On this graph, we want to find the minimum cost to get from the initial state \(0\) (all lights off) to the target state \(2^N - 1\) (all lights on).
Handling Dangerous Patterns
The constraint that “the state immediately after an operation must not match any dangerous pattern” means we cannot visit the vertices corresponding to dangerous patterns. Therefore, we should avoid using edges that transition into states corresponding to dangerous patterns (i.e., we never enter those vertices). In practice, if the next state \(v\) is a dangerous pattern, we simply skip that transition.
Note that since the problem guarantees that the initial state and the target state (all lights on) are not dangerous patterns, the start and end vertices are always valid.
Issues with a Naive Approach
With approximately \(260,000\) states and edge-dependent (weighted) costs, a simple BFS cannot find the correct minimum cost. Since all costs are positive, we can efficiently find the shortest path using Dijkstra’s algorithm.
Algorithm
We use Dijkstra’s algorithm.
- For each switch panel, precompute the bitmask \(\text{mask}_j\) of the affected lights and its cost \(C_j\).
- Record the dangerous patterns in a
forbiddenarray (setting the corresponding states to \(1\)). - Initialize
distof the starting state \(0\) to \(0\), and run Dijkstra’s algorithm using a priority queue (min-heap).- Once a state \(u\) is popped, compute the next state \(v = u \oplus \text{mask}_j\) for each switch panel \(j\).
- If \(v\) is a dangerous pattern, skip it.
- Otherwise, update \(\text{dist}[v]\) with \(\text{dist}[u] + C_j\).
- If
distof the target state \(2^N - 1\) is updated, output it; otherwise, if it remains \(\infty\), output \(-1\).
Since XOR is its own inverse (\(u \oplus \text{mask}_j \oplus \text{mask}_j = u\)), the edges can be considered undirected, but in practice, it is sufficient to simply try all panels from each vertex.
Complexity
Let the number of vertices be \(V = 2^N\). Since there are \(M\) outgoing edges from each vertex, the number of edges is \(E = M \cdot 2^N\).
- Time Complexity: \(O(M \cdot 2^N \log(2^N)) = O(N M 2^N)\) (using Dijkstra’s algorithm with a priority queue)
- Space Complexity: \(O(2^N)\) (for the
distarray andforbiddenarray)
Since \(N \leq 18\) and \(M \leq 20\), the number of edge relaxations is at most \(20 \times 262144 \approx 5 \times 10^6\), which runs well within the time limit.
Implementation Details
Manage states as bitwise integers: Map the state of light \(i\) to the \(i\)-th bit, constructing it like
mask |= (1<<i). Although the light indices in the input are 1-based, it is easier to handle them as 0-based bit positions in your code.Be careful with the upper bound of the cost: Since \(C_j \leq 10^6\) and there are many edges, the total cost can exceed the range of a standard 32-bit
int. Store the distances inlong long(the problem description also guarantees that the answer fits in a signed 64-bit integer).Check dangerous patterns in \(O(1)\) time with an array lookup: There are at most \(10^5\) dangerous patterns, but if you store them in a boolean
forbiddenarray of size \(2^N\), checking whether a state is dangerous can be done in constant time during transitions.Early termination when the target is reached: The moment the target state is popped from the priority queue, its shortest distance is finalized. You can
breakearly to speed up the execution.Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, M, K;
scanf("%d %d %d", &N, &M, &K);
vector<int> mask(M);
vector<long long> cost(M);
for(int j=0;j<M;j++){
int m=0;
for(int i=0;i<N;i++){
int b; scanf("%d",&b);
if(b) m |= (1<<i);
}
long long c; scanf("%lld",&c);
mask[j]=m;
cost[j]=c;
}
int total = 1<<N;
vector<char> forbidden(total, 0);
for(int k=0;k<K;k++){
int f=0;
for(int i=0;i<N;i++){
int x; scanf("%d",&x);
if(x) f |= (1<<i);
}
forbidden[f]=1;
}
const long long INF = LLONG_MAX;
vector<long long> dist(total, INF);
int start = 0;
int target = total-1;
dist[start]=0;
priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq;
pq.push({0,start});
while(!pq.empty()){
auto [d,u] = pq.top(); pq.pop();
if(d>dist[u]) continue;
if(u==target) break;
for(int j=0;j<M;j++){
int v = u ^ mask[j];
if(forbidden[v]) continue;
long long nd = d + cost[j];
if(nd < dist[v]){
dist[v]=nd;
pq.push({nd,v});
}
}
}
if(dist[target]==INF) printf("-1\n");
else printf("%lld\n", dist[target]);
return 0;
}
This editorial was generated by claude4.8opus-high.
投稿日時:
最終更新: