Hello everyone
I'm wondering how to find the number of distinct shortest paths in an undirected unweighted graph !!
any advice !! :D
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | adamant | 157 |
6 | awoo | 157 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | djm03178 | 153 |
Hello everyone
I'm wondering how to find the number of distinct shortest paths in an undirected unweighted graph !!
any advice !! :D
Название |
---|
It shouldn't be hard. Just do a bfs and then you have dist[i] = the distance between source and node i. Sort the nodes by their dists and then compute some dp value for each node, in the order of the sorting, with dp[i] representing the number of distinct shortest paths from source to node i. The recurrence would be: dp[i] = sum of dp[j] so that dist[j] + 1 = dist[i] and you have an edge from j to i. You can see that using the sorting order, you have computed all dps that you had interest in beforehand, so it would work. The final complexity is O(N+M) because sorting isn't just a standard sort. You could use count sort, or even take the nodes in the order they were popped from the queue. Of course, the answer is dp[N] where N is the destination and initializing requires you to set dp[source] = 1