Kimbap Heaven and Road Hell
Time limit2sMemory limit1024 MB
Given roads of 2, 3, or 6 minutes, determine whether a walk from district 1 can reach district N at exactly K minutes.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Number theory
- Solved
- No attempts yet
Problem
Sejin lives in a town with districts and roads connecting them. The districts are numbered 1 to . The roads come in three colors: red, yellow, and blue.
Sejin loves kimbap so much that instead of going to Kimbap Heaven himself, he built a delivery robot that can order food for him. The robot never stops moving. It takes exactly 2, 3, and 6 minutes to cross a red, yellow, or blue road, respectively.
Sejin just called Kimbap Heaven and said he will come pick up the food in exactly minutes. He plans to start the robot from his location right now. Given the town layout, Sejin's location, and the kimbap shop's location, determine whether the robot can follow a route that reaches the shop at exactly minute .
The robot may revisit districts or travel the same road more than once to make the time work out. It may also pass through the district with the shop before arriving. You only need to check whether the robot can be at the shop's location exactly minutes after it starts.
Input
The first line contains , , and , separated by spaces.
Each of the next lines contains , , and , separated by spaces. This means there is a road between districts and that takes minutes to cross.
All roads can be traveled in both directions. Two roads of the same color are never given between the same pair of districts. The roads are given so that every district can be reached from district 1.
The robot always starts at district 1, and the kimbap shop is always at district .
Output
Print YES if the robot can reach the shop at exactly minute , and NO if it cannot.