27 · Shortest Paths

#1976 Number of Ways to Arrive at Destination

Mediumsolve on LeetCode ↗

frequently reported atGoogleMeta

the problem

In plain words

A city has n intersections (0..n − 1) and two-way roads [u, v, time]. Count how many different routes from 0 to n − 1 take the shortest possible time. Return the count modulo 10⁹ + 7.

in n = 7, roads = [[0,6,7],[0,1,2],[1,2,3],[1,3,3],[6,3,3],[3,5,1],[6,5,1],[2,5,1],[0,4,5],[4,6,2]]

out 4

The shortest time is 7, achieved by 4 different routes.

in n = 2, roads = [[1,0,10]]

out 1

  • 1 ≤ n ≤ 200
  • n − 1 ≤ roads.length ≤ n·(n − 1)/2
  • 1 ≤ time ≤ 10⁹
  • Connected, at most one road between two intersections

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #743
      Network Delay Time
      Mediumin Shortest Paths
    2. #2050
      Parallel Courses III
      Hardin Topo Sort
    3. #62
      Unique Paths
      Mediumin Dynamic Programming