Magical Portals
Time limit: 2 secMemory limit: 256 MBData Structures, Graph Theory, Dynamic Programming
Problem Statement
In the mystical realm of Algorithmland, a wise wizard has discovered a series of magical portals connecting different cities. There are cities numbered from to , and exactly portals. Each portal allows travel in one direction only, taking a certain amount of time.
The wizard's quest is to travel quickly from city to city . Your task is to determine the minimum amount of time required to achieve this journey. If no valid route exists from city to city , output .
Input Format
- The first line contains two space-separated integers, and , where is the number of cities and is the number of magical portals.
- Each of the next lines contains three space-separated integers: , , and . The integer denotes the starting city, denotes the destination city of the portal, and is the time (in minutes) it takes to travel through that portal.
Output Format
- Output a single integer: the minimum time required to travel from city to city . If there is no possible route, output .
Examples
Input
4 4
1 2 5
2 3 10
1 4 100
3 4 5
Output
20
Input
3 1
2 3 4
Output
-1
Code Editor
Submit a solution. Authentication is required to create submissions.