Space smugglers
Practice
4.7 (17 votes)
Algorithms
Approved
Dijkstra's algorithm
Graphs
Medium
Shortest path algorithm
Problem
89% Success 1551 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Nathan Reynolds is a famous smuggler and captain of a spaceship "Serenade". He was offered a potentially dangerous job on Ariel, one of the border planets of the star system. But to save all the "honest" earnings of the previous adventures, he decided to store them on one of the planets on the way to the border.

Star system is represented by n planets and m space tunnels between them. Space tunnel is one-way wormhole to travel from one planet to another, requiring an amount of gravitonium to perform the gravity jump. There may be several space tunnels between two planets, but no space tunnel leads to the same planet it starts from.

Your task as a first officer is to find the minimum amount of gravitonium to travel from the base planet to Ariel, visiting some other planet to store the earnings, and return back to base, picking up the earnings on the way back.

Note, that storing the earnings in a base planet or the planet, where the job is taking place, is not allowed. But it's allowed to visit Ariel with the earnings as long as you are not doing a job on this planet.

Input format

The first line of input contains four integers n, m, s and t — number of planets in a star system, number of space tunnels, the index number of the base planet and the index number of Ariel, respectively.

Each of the next m lines contains three integers u, v and g — the planet, where the space tunnel starts, the planet, where the space tunnel goes to, and the amount of gravitonium required to travel through the space tunnel, respectively ().

Constraints

\( 2 \le n \le 10 ^ 5 \)
\( 1 \le m \le 10 ^ 5 \)
\( 1 \le s, t \le n \), \( s \ne t\)
\( 1 \le u, v \le n \)
\( 1 \le g \le 10 ^ 3\)

Output format

Output the minimum time required to travel from the base planet to Ariel, visiting some other planet to store the earnings, and return back to base, picking up the earnings on the way back, or output -1, if it's impossible.

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
3 votes
Tags:
AlgorithmsGraphsShortest Path Algorithms
Points:20
11 votes
Tags:
Basic ProgrammingEasyOpenApprovedHash table
Points:30
24 votes
Tags:
AlgorithmsGraphsMediumShortest Path Algorithm