1 Star 1 Fork 0

bensonrachel/Atcoder_algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
D - Number of Shortest paths(BFS).py 1.42 KB
一键复制 编辑 原始数据 按行查看 历史
# -*- 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))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bensonrachel/atcoder_algorithm.git
git@gitee.com:bensonrachel/atcoder_algorithm.git
bensonrachel
atcoder_algorithm
Atcoder_algorithm
master

搜索帮助