Metro

2.1

11 votes
Algorithms, Graphs, Medium, Shortest Path Algorithm
Problem

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 ith subway goes to stations ui,1,ui,2,ui,3,...,ui,ki(in order) and this train takes wi,j seconds to travel from ui,j to ui,j+1(for 1im and 1j<ki).

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


Input 

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

Next 3m lines describe subway lines.

The first line contains ki,ti and next line contains ui,1,ui,2,ui,3,...,ui,kiand the next line contains w1,w2,...,wki1.

The last line containts start,end.

It is guaranteed startend 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

1n,m100 000

1ui,jn

2ki

k1+k2+...+km100 000

0ti109

1wi,j109

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In second 0 get on first subway line and in next second we arrive to station 2 and we can get on second line and arrive to 4 in second 2

Editor Image

?