Natural Navigation
Time limit6sMemory limit1024 MB
Find the least walking time from intersection 1 to n when the app picks colours and the user takes the worst footpath for each colour.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Hash map
- Solved
- No attempts yet
Problem
You want to build an app to help people navigate through a large botanic garden. This is difficult, because there are many winding footpaths and intersections offering many choices, making traditional directions such as "turn right" or "move further north" unsuitable. Instead, the app should rely on the garden's greatest resource: the numerous exotic plants and their diverse colours. Whenever a user is at an intersection, the app will know where they are and will display one particular colour accordingly. The user will then follow a footpath where this colour is visible. If the colour can be spotted along multiple footpaths originating from the intersection, the user is free to choose any of these footpaths.
You have been given a perfect model of the botanic garden, consisting of intersections (numbered from to ) and footpaths going between those. To keep order, each footpath can only be used in the given direction. Currently, the plants are exhibiting different colours (numbered from to ) and for each footpath, you are given a list of all the colours that are visible along it when viewed from the intersection where it starts. A user is currently at intersection and wants to navigate to intersection . You can assume that the user will follow the app's directions perfectly, but whenever faced with multiple options (because the given colour is visible along multiple footpaths), you have to assume they will make the worst possible choice. How long will it take to reach the target when your app gives the best possible instructions?
Input
The first line contains (), (), and ().
Then footpaths follow, each described by two lines:
- One line with three integers , , and (, ). The footpath leads from intersection to intersection and takes seconds to walk.
- One line with an integer (), followed by distinct integers (). These are the colours visible along the footpath.
The sum of over all footpaths does not exceed . A footpath can lead back to its starting intersection, and several footpaths can connect the same pair of intersections. Intersection is not guaranteed to be reachable from intersection .
Output
If the user cannot be led to intersection , print impossible. Otherwise, print a single integer: the time in seconds to reach intersection . Only the time spent walking along the footpaths is counted.