Блог пользователя ALuca_Rd

Автор ALuca_Rd, история, 2 года назад, По-английски

Given a graph with N nodes with each node numbered from 1 to N and M edges and a target vertex T. Find the number of different ways to reach target vertex T from vertex numbered 1.

2 <= N <= 2 * 10^5, M = min( 2 * 10^5, N * ( N - 1 ) / 2 )

Time limit — 5sec

Compute the answer modulo 10^9 + 7.

  • Проголосовать: нравится
  • +12
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by ALuca_Rd (previous revision, new revision, compare).

»
2 года назад, # |
  Проголосовать: нравится +33 Проголосовать: не нравится

Infinite.

»
2 года назад, # |
Rev. 2   Проголосовать: нравится -8 Проголосовать: не нравится

Here you can use topological sorting + dp for directed acyclic graph. Link

»
2 года назад, # |
Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

You can find the number of shortest paths from using this.

First, how can we find the length of the shortest path from Vertex 1 to Vertex N? Since the every edge has weight 1, the question can be answered with a BFS (Breadth-First Search) in a total of O(N+M) time.

The original problem can similarly be solved with BFS in a total of O(N+M) time, in which we compute not only the shortest distance from the starting vertex but also the number of the paths accomplishing the distance.

When finding the shortest distance with BFS, we compute the array of shortest distance dist by:

Explanation
Code
»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

You can refer to this Blog it may help you — https://www.baeldung.com/cs/graph-number-of-shortest-paths