You are on page 1of 2

Hopping over the Internet (Task-1)

Input file: standard input


Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes

Difference between both the sub tasks is the constraints.


There are a total of n routers in this network and you can make connections between any of them. They
are numbered 1 to n and packet transmission between any two routers is only possible through these
specially created connections. Your message is in the 1st router whereas the destination is in the nth
router. Due to the complexity of the network’s transmissions techniques, packet transmission between
different routers can take an unequal amount of transmission time (in minutes).
You want to send your message (consider it a packet) from router 1 to router n.
Also, some of the routers handle priority connections and are too busy at specific time instances. When
they are busy with priority connections they won’t transmit your packet to the next router. In order to
pass through them, your packet has to wait there until the time on which they are not busy.
If your packet arrives at a router at time t and it is marked busy at time t, then the packet will wait until
the router is not busy.
Since no one likes delay over the internet, you want your message to be delivered at the earliest. With
the information about the connections, their transmission times and busy minutes (the minutes on which
they will be busy), you have to find the fastest route for your packet to reach the destination (nth router).

Input
The first line contains two space-separated integers: n, the number of routers and m, the number of
connections.
Then m lines follow, ith line contains three integers, routers ai and bi , connected through the ith
connection and ci , the transmission time between the mentioned routers.
Then n lines follow, ith line contains ki , number of time instances when the ith routers are busy. Then ki
space separated integers tij follow in sorted order. tij means that at this timestamp the router is in the
busy state and your packet has to wait for this second (if it is at this router at this timestamp).
For this sub task all ki0 s will be 0. All routers will be free for transmission at any given time.
Constraints:
• 2 ≤ n ≤ 105
• 0 ≤ m ≤ 105
• 1 ≤ ci ≤ 104
• Sum of ki = 0
• 1 ≤ ai , bi ≤ n
• 1 ≤ tij ≤ 109

Output
Print a single number — the least amount of time your packet needs to get from router 1 to router n. If
the packet can’t get to router n in any amount of time, print number −1.

Example
standard input standard output
3 2 7
1 2 3
2 3 4
0
0
0

Page 1 of 2
Note
It is guaranteed that there would be at most one connection between any pair of routers.
In the above example, the packet can jump from router 1->2 and then from 2->3 to reach the destination
in a total of 7 seconds. (3+4)
HashMaps and HashSets are not allowed for this problem.

Page 2 of 2

You might also like