The White Rabbit's Pocket Watch
Time limit2sMemory limit512 MB
Given walks whose total length is known only modulo 13, recover each edge's true weight in 1..12 and find the shortest path from A to R.
- Level
Medium7 of 10
- Topics
- Graph, Number theory, Shortest path, Math
- Solved
- No attempts yet
Problem
The White Rabbit's pocket watch shows only the hours 0 to 12, and past 12 it returns to 0. When a trip ends, the Rabbit writes down whatever number the pointer rests on. A trip that really took 14 hours leaves the pointer on 1, so his logbook records 1. Every duration in the logbook is therefore the real duration taken modulo 13.
Wonderland has locations. There is at most one track directly joining two locations, and a track takes the same real time in both directions. The real time of one track is an integer between 1 and 12.
The logbook holds trips. One entry has the time read off the watch, the number of locations passed, and the order in which they were passed. The same location can appear several times. Two locations that are neighbours in the order are joined by a direct track, and the tracks that appear in the logbook are all the tracks of Wonderland.
Alice is at location and wants to reach the Rabbit's hole at location . Find the smallest real time of a route from to .
The logbook alone fixes the real time of every track, and at least one route from to exists.
Input
The first line contains the integers , , and separated by spaces. is the number of distinct locations, is the location where Alice is, is the location of the Rabbit's hole, and is the number of trips written in the logbook. Locations are numbered from 1 to .
Each of the next lines describes one trip in the format d p a1 a2 ... ap, where is the trip time the Rabbit read off the watch, is the number of locations passed, and is the order in which they were passed.
Output
Print one integer, the shortest real time it takes Alice to reach the Rabbit's hole.
Constraints
- There are at most 200 distinct tracks.
- The real time of one track is an integer with .