Metro
Practice
2.1 (11 votes)
Algorithms
Graphs
Medium
Shortest path algorithm
Problem
82% Success 909 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

A city subway line has become huge and it is hard to take the shortest path through them. You have to find the shortest path in subway lines. In the second \(0\), you are in the station \(start\) and you want to go to the station \(end\).

The city has \(n\) stations. The subway has \(m\) lines. Each subway line goes to some stations.

The $$i-th$$ subway goes to stations \(u_{i, 1}, u_{i, 2}, u_{i, 3}, ..., u_{i, k_i}\)(in order) and this train takes $$w_{i, j}$$ seconds to travel from \(u_{i, j}\) to \(u_{i, j+1}\)(for \(1 \le i \le m\) and \(1 \le j < k_i\)).

Trains are ready for the passengers to get in, but the last train goes in the second $$t_i$$(and you are allowed to board it in between the path).


Input 

The first line contains $$n, m$$(in order).

Next $$3*m$$ lines describe subway lines.

The first line contains $$k_i, t_i$$ and next line contains \(u_{i, 1}, u_{i, 2}, u_{i, 3}, ..., u_{i, k_i}\)and the next line contains \(w_1, w_2, ..., w_{k_{i - 1}}\).

The last line containts $$start, end$$.

It is guaranteed \(start \neq end\) and no subway line intersects itself.


Output 

Print the shortest path in subway lines from the station \(start\) to \(end\).

If there is no way from \(start\) to \(end\), print -1.

 

Constraints

\(1 \le n, m \le 100\ 000\)

\(1 \le u_{i, j} \le n\)

\(2 \le k_i\)

\(k_1 + k_2 + ... + k_m \le 100\ 000\)

\(0 \le t_i \le 10^9\)

\(1 \le w_{i, j} \le 10^9\)

 

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:
GraphsMediumShortest Path Algorithm
Points:30
24 votes
Tags:
AlgorithmsGraphsMediumShortest Path Algorithm
Points:30
12 votes
Tags:
AlgorithmsGraphsMediumShortest Path Algorithm