A courier network has n depots, numbered 0 to n - 1, connected by m two-way roads.
Road i connects depots u and v and takes w minutes to travel. Multiple roads between the same
pair of depots may exist; each is a different road even if it connects the same two depots, so using a
different road counts as a different route even between an identical pair of stops.
A route from a starting depot s to a destination depot t (with s != t) is a sequence of roads
that never visits the same depot twice. Among all routes from s to t, consider only the ones with
the minimum possible total travel time. Print how many such fastest routes exist, modulo
1000000007. If t cannot be reached from s at all, print -1 instead.
Line 1: two integers n m.
Next m lines: three integers u v w -- an undirected road between depots u and v taking w
minutes (depots are 0-indexed).
Last line: two integers s t.
A single integer: the number of fastest routes from s to t, modulo 1000000007, or -1 if t is
unreachable from s.
Example 1
Input
4 4 0 1 1 0 2 1 1 3 1 2 3 1 0 3
Expected
2
Explanation
Both routes 0 -> 1 -> 3 and 0 -> 2 -> 3 cost 1+1=2 and no cheaper route exists, so there are 2 distinct fastest routes.
Example 2
Input
3 1 0 1 5 0 2
Expected
-1
Explanation
Depot 2 is not connected to depot 0 at all, so no route exists and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →