It's not a Bug, it's a Feature!
InterviewTime limit1sMemory limit128 MB
Each bug state is a bitmask; find the shortest total patch time from all bugs present to no bugs, where patches have presence and absence preconditions.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Bit manipulation, BFS
- Solved
- No attempts yet
Problem
It is a curious fact that consumers buying a new software product generally do not expect the software to be bug-free. Can you imagine buying a car whose steering wheel only turns to the right? Or a CD player that plays only CDs with country music on them? Probably not. But for software systems it seems to be acceptable if they do not perform as they should. In fact, many software companies have adopted the habit of sending out patches to fix bugs every few weeks after a new product is released (and even charging money for the patches).
Tinyware Inc. is one of those companies. After releasing a new word processing software this summer, they have been producing patches ever since. Only this weekend did they realize a big problem with the patches they released. While every patch fixes some bugs, it often relies on other bugs being present in order to be installed. This happens because, to fix one bug, a patch exploits the special behavior of the program caused by another bug.
More formally, the situation looks like this. There are bugs in the software, and patches . To apply patch , all bugs in must be present and all bugs in must be absent (of course ). Applying the patch then removes the bugs in (those that were present) and introduces the new bugs in (again with ).
Starting from the original version, which contains every bug in , is it possible to apply a sequence of patches that results in a bug-free version of the software? If so, and assuming each patch takes a given time to apply, how long does the fastest sequence take?
Input
The input contains several product descriptions. Each description starts with a line containing two integers and , the number of bugs and the number of patches, respectively, with and . This is followed by lines describing the patches in order. Each line contains an integer, the time in seconds it takes to apply the patch, followed by two strings of characters each.
The first string describes which bugs must be present or absent before the patch can be applied. Its -th character is + if bug must be present, - if bug must be absent, and 0 if it does not matter whether the bug is present.
The second string describes which bugs are fixed and introduced by the patch. Its -th character is + if bug is introduced by the patch, - if bug is removed by the patch (when it was present), and 0 if bug is not affected (if it was present before, it still is; if it wasn't, it still isn't).
The input is terminated by a description whose first line is 0 0; this description must not be processed.
Output
For each product description, first print a line Product X, where X is the product's number (starting from 1). Then, if there is a sequence of patches (a patch may be used several times) that removes all bugs from a product that starts with all bugs present, print Fastest sequence takes S seconds., where is the minimum total time. Otherwise, print Bugs cannot be fixed..
Separate the output of consecutive products with a blank line.