Dangerous Dungeon
Practice
4.3 (7 votes)
Approved
Graphs
Math
Medium
Open
Shortest path algorithm
Problem
43% Success 992 Attempts 30 Points 3s Time Limit 256MB Memory 1024 KB Max Code

See Russian Translation

David is playing a video game set in a dungeon. The dungeon has n interesting points and m one way tunnels connecting those points. The i-th tunnel starts at point ai and ends at point bi and takes ci units of time to traverse. Note that it may be possible that there are two tunnels connecting the same pair of points, and even more strangely, a tunnel that goes back to the same point.

David is currently at the point labeled 0, and wishes to travel to the point n-1 using the smallest amount of time.

David discovered that some points become dangerous at some moments of time. More specifically, for each point, David found an integer oi such that point i is dangerous at all times t satisfying t = oi modulo p. The integer p is the same for every single intersection.

David needs to always be in constant motion, and can't wait at a point. Additionally, he will always be safe in a tunnel, but cannot arrive at a point at the moment it becomes dangerous.

Help David determine the fastest time he can reach his destination.

Input Format:

The first line of input will contain three space separated integers n,m,p.
The next line of input will contain n space separted integers oi.
The next m lines of input will each contain 3 integers ai, bi, ci

Output Format:

If it is impossible for David to reach point n-1, print -1. Otherwise, print the smallest amount of time it will take David to reach n-1.

Constraints:

For all subtasks:
0 ≤ ai, bin-1
0 ≤ ci ≤ 109
0 ≤ oip-1

25 pts:
2 ≤ n ≤ 50
1 ≤ m ≤ 250
2 ≤ p ≤ 50

30 pts:
2 ≤ n ≤ 100
1 ≤ m ≤ 10000
2 ≤ p ≤ 500

30 pts:
2 ≤ n ≤ 50
1 ≤ m ≤ 2500
2 ≤ p ≤ 109

15 pts:
2 ≤ n ≤ 1000
1 ≤ m ≤ 10000
2 ≤ p ≤ 109

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
9 votes
Tags:
AlgorithmsBinary SearchFloyd Warshall AlgorithmGraphsMediumShortest Path Algorithm
Points:30
11 votes
Tags:
ApprovedBasic ProgrammingGraphsMediumOpenShortest Path Algorithm
Points:30
3 votes
Tags:
Shortest Path AlgorithmsGraphsAlgorithms