The Mountain of Gold?
Time limit1sMemory limit256 MB
Decide whether portal hops from mountain 0 can return to mountain 0 at a strictly earlier time.
- Level
Medium4 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
Old stories put rich gold deposits on Gunung Ledang in Malaysia, and they drew traders from as far as Greece and China. In the 14th century the Chinese seafarers who sailed the Straits of Melaka called it Kim Sua, the golden mountain. The name Gunung Ledang, given during the Majapahit empire, means the mountain seen from afar.
Legend says the princess of Gunung Ledang travelled back to the time when the earth was made and hid a huge amount of gold in the mountain. She could turn any pool she bathed in into a portal that joins two points in space and time. A historian later found many such pools near mountains around the world and named them Ledang Pools.
A Ledang Pool has these properties.
- One pool is a one way portal between two different mountains.
- Travelling through a pool takes no time.
- A mountain may hold the end points of several pools.
- Starting from Ledang Mountain, a sequence of pools reaches every mountain.
- No pool has both of its end points on the same mountain.
- Each pool has a fixed time difference between its end points. Travelling through one pool may drop the traveller 42 years in the past at the other end.
No gold is on Ledang Mountain today, so the historian believes it is hidden on Ledang Mountain in the past. He wants to start from Ledang Mountain in the present, hop through two or more pools, and arrive back at Ledang Mountain at a moment strictly before he left. How far in the past he lands does not matter. Decide whether such a trip exists.
Input
The first line contains the number of test cases .
The first line of each test case contains the number of mountains that hold a pool end point, , and the number of pools, . The mountains are numbered from to , and mountain is Ledang Mountain in Malaysia.
Each of the next lines contains three integers , , describing one pool. Entering that pool on mountain puts the traveller on mountain , years later. A positive means the future and a negative means the past.
Output
For each test case, print one line in the form Case #X: Y. is the test case number, counted from 1. is possible if the historian can reach Ledang Mountain in the past, and not possible otherwise.
Constraints
- , ,
- Every mountain is reachable from mountain .
- Several pools may join the same pair of mountains in the same direction.