A Journey to Greece
Time limit2sMemory limit1024 MB
Plan a round trip from Athens that visits every listed site within time G, using at most one fixed-time taxi jump.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Shortest path, Bit manipulation
- Solved
- No attempts yet
Problem
Tim has wanted to visit Greece for a long time. He already bought his flights to and from Athens, and he made a list of the historical sites he wants to see, such as Olympia and Delphi. Recent politics made public transport in Greece complicated. To keep the citizens happy with their new government, many short bus and train lines were opened, and they carry people around their own neighbourhood, to work or to the doctor. At the same time the long distance trains that suit tourists were shut down because they cost too much. That is bad news for Tim, who really likes travelling by train. He had even bought the Greece Card for Public Conveyance (GCPC), which makes every train and bus free for him.

The figure above draws the first example. Tim's tour has length 18.
Tim still wants to make his trip, but changing between local buses and trains is slower than he expected, so he wants to know whether he can see every site inside the window his flights leave him. His schedule is tight. He does keep some emergency money, enough for a single ticket for a special Greek taxi service that carries you from any point in Greece to any other point in a fixed amount of time.
Assume Tim never waits at a station for the next bus or train. The journey starts and ends in Athens. Tim may pass a place several times, and he may pass through a place without stopping. The journey fits the schedule if the travel times plus the times he spends at the sites add up to at most . He may use the taxi ticket at most once, at any moment of the journey, and that ride takes no matter where it starts and where it ends.
Decide whether Tim can see every site in time, and if he can, whether he needs the taxi ticket.
Input
The first line contains five integers , , , and . Here is the number of places in Greece, is the number of sites Tim wants to visit, is the number of connections, is the total time Tim can spend in Greece, and is the time one taxi ride takes (, , , ).
Each of the next lines contains two integers and . Here is a place Tim wants to visit and is the time he spends at that site (, ). The places are distinct.
Each of the next lines describes one connection with three integers , and . Here and are the two ends of the connection and is the time it takes (, ).
Every connection runs in both directions. Tim's journey starts and ends in Athens, which is always place 0.
Output
Print one line. Print impossible if Tim cannot visit all sites in time, possible without taxi if he can visit them all without the taxi ticket, and possible with taxi if he needs the taxi ticket.