Super Bus
Time limit1.5sMemory limit512 MB
Find the shortest travel time from stop 1 to stop N, where a road can be used only when the sum of its two endpoints' disaster codes is prime.
- Level
Medium5 of 10
- Topics
- Shortest path, Graph, Number theory
- Solved
- No attempts yet
Problem
Bo-seon, who is visiting Jinju, boards a bus to reach the Jinyangho Observatory, which she reserved in advance. She arrives at a nearby bus stop, but the bus information display shows no arrivals at all. Confused, she checks around until a message arrives from Jun-hee, a college classmate.
How does it taste?
Jun-hee sent it. In college, Jun-hee was a risotto fan who had to eat gukbap every day because of Bo-seon, a gukbap fanatic, and has held a grudge ever since. When Jun-hee learned that Bo-seon was visiting Jinju, Jun-hee used hacking skills to break into the bus information system and stop city buses from running.
Jinju treated the hacked bus system as a disaster and started running Super Bus. To keep Super Bus running smoothly, all other means of transportation were banned.
Jinju has bus stops numbered from 1 in order. There are bidirectional roads, each joining two different bus stops, with a fixed travel time on Super Bus. Two bus stops may be joined by two or more roads. Each bus stop has a disaster code, a positive integer at most . Super Bus only runs on a road between two bus stops when the sum of their disaster codes is a prime. Here, a prime is an integer of at least 2 whose only divisors are 1 and itself.
Bo-seon starts at stop 1, and the Jinyangho Observatory is at stop . The stops are far apart, so Super Bus is the only way to travel. Bo-seon can board any Super Bus as soon as she arrives at a stop, and boarding or alighting takes no time. Find the shortest time for Bo-seon to reach stop .
Input
The first line contains the number of bus stops and the number of bidirectional roads , separated by a space. (; )
The second line contains the disaster codes of the bus stops, separated by spaces. ()
From the third line onward, the next lines each contain , , and , separated by spaces. This describes the -th bidirectional road, which joins stop and stop and takes time to travel on Super Bus. (; )
All input numbers are integers.
Output
On the first line, print the shortest time to reach the Jinyangho Observatory. If it cannot be reached, print Now where are you?.
Hint
Python 3 users are advised to submit with PyPy3.