Flight Plans
Time limit1sMemory limit512 MB
Each airport lists either its outgoing destinations or its non-destinations; find the fewest flights from s to t in the resulting implicit graph.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Implementation, Hash map
- Solved
- No attempts yet
Problem
As you are probably aware, flight pricing can sometimes be surprisingly complex. For example, it can often be cheaper to take a much longer flight with several legs instead of flying directly between two airports. One of the reasons pricing seems to be complex is that airlines often try to obfuscate exactly how pricing works, in order to ensure that their customers choose more expensive flights.
One particular airline has decided to take this obfuscation to the next level; they do not even offer an automated search of their flights. Instead, they describe their flights in a very peculiar format. For every one of their airports (which are numbered between and ), they list either:
- what airports they travel to from this airport, or
- what airports they do not travel to from this airport.
To compensate for this complexity, the airline sets the price of every direct flight between two airports to the same amount.
Can you write a program that, given the descriptions of all the flights the airline provides, determine the minimum number of flights required to travel from airport to airport ?
Input
The first line of input contains an integer , the number of airports, and the two integers and (, ).
The next lines each describe the outgoing flights of an airport, starting with airport . The line starts with a letter. If this letter is N, you will get a list of all destination airports from this airport. If this letter is C, you will get a list of all airports that are not destinations from this airport.
Following this letter is an integer , the number of airports in the list. Finally, there will unique numbers () on the line, the airports in the list.
The sum of over all airports is at most .
Output
Output a single integer, the minimum number of flights required to travel from airport to airport .
If there is no path, output "impossible".