Quantum
Time limit1sMemory limit128 MB
Given up to 32 quantum operations on length-L bit words and their costs, find the minimum cost to transform each source word into a target word, or report it impossible.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
A research team is developing a new way of storing and manipulating data on magnetic disks. The method lets quantum operations act on the sectors of a disk. Each quantum operation costs a certain amount of energy, and the more energy the storage unit consumes, the warmer it gets. Given a set of available quantum operations and their costs, write a program that computes the lowest possible total cost of transforming a given binary word into a desired binary word.
A binary word has length . Every quantum operation is a string of the same length , built from the four letters:
N— does nothing (leaves the bit unchanged);F— flips (inverts) the bit;S— sets the bit to1;C— resets the bit to0.
The -th letter of an operation acts on the bit at position of the binary word. Applying an operation transforms every position of the word simultaneously according to its letters. You may apply operations in any order and any number of times. A word is transformed by applying a sequence of operations to it, and the total energy cost equals the sum of the costs of the operations performed. For each requested pair of words, find the minimum total cost to turn the first word into the second, or report that it is impossible.
Input
The first line contains a single integer (), the number of test cases. Each test case is given as follows:
- One line with three integers , and separated by single spaces, where () is the length of the binary words and operations, () is the number of available quantum operations, and () is the number of binary words to transform.
- Then lines follow, each containing the definition of one quantum operation (a string of length over
N,F,S,C) and its energy cost (), separated by a single space. - Then lines follow, each containing two binary words of length separated by a single space. The first word must, when possible, be transformed into the second using the available operations. Binary words are written as sequences of
0s and1s.
Output
For each test case, print one line listing the minimum energy cost of transforming each binary word, in the same order as the input, separated by single spaces. If a binary word cannot be transformed into its target, print NP (not possible) in its place instead of a cost.