代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# @project : 《Atcoder》
# @Author : created by bensonrachel on 2021/9/1
# @File : D - Number of Shortest paths(BFS).py
# https://atcoder.jp/contests/abc211/tasks/abc211_d
# How many paths are there in which you can get from City 1 to City N as early as possible?
# 问题解释ans:https://stackoverflow.com/questions/15211611/number-of-shortest-paths-in-a-graph#
def re():
bfs = [1]
dist = [None] * (n + 1)
dist[1] = 0
cnt = [0] * (n + 1)
cnt[1] = 1
#初始化
for v in bfs:
for vv in char[v]:
if (dist[vv] is None):
dist[vv] = dist[v] + 1
bfs.append(vv)
cnt[vv] = cnt[v]
elif (dist[vv] == dist[v] + 1):#(*)说明有从其他地方到达vv的最短路径和通过v到达vv的最短路径相等,所以路径数目就是加起来
cnt[vv] += cnt[v]
return cnt
"""
(*)处解释: 因为用的是BFS,所以dist[vv] > dist[v] + 1 ,这种情况不会出现.
如果是dist[vv] < dist[v] + 1,这种情况下不需要做任何事,因为这个v到vv的路径不是最短路径,所以不需要更新
"""
if __name__ == "__main__":
n, m = map(int, input().split())
char = [[] for _ in range(n + 1)]
for _ in range(m):
a, b = map(int, input().split())
char[a].append(b)
char[b].append(a)
res = re()
print(res[n] % (10 ** 9 + 7))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。