Empire
InterviewTime limit1sMemory limit256 MB
Find the fastest route from A to B whose total hull damage stays strictly below K.
- Level
Medium5 of 10
- Topics
- Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
Jiyong tore up a calculus textbook and built a raft of thickness , thick enough to carry one person. He now plans to leave port A and sail to the uninhabited island B to found his empire.
The sea holds islands and sea routes. Nothing outside a sea route is passable, so Jiyong sails from island to island. Sea route takes time to cross and shaves centimeters off the raft.
Once the total of the crossed routes reaches , the raft falls to a thickness of 0cm or less and Jiyong, who cannot swim, does not survive. The voyage is safe only while the total of the crossed routes stays below .
Find the safe voyage from A to B that takes the least time and print that time. He may pass through the same island or the same sea route more than once, and every crossing adds its time and its damage again.
Input
The first line has three integers , , . (, , )
Each of the next lines describes one sea route as , , , . (, , , ) A two-way sea route connects island and island , crossing it takes time , and it shaves off the raft. Several sea routes may connect the same pair of islands.
The last line has the start and the destination . (, )
Output
Print the minimum time of a safe voyage from A to B, or -1 if Jiyong cannot sail there safely.